开发者社区> 问答> 正文

我想让每个用户和他们最后一次付款。

我想让每个用户和他们最后一次付款。我有2代表在这里users和finances。我尝试添加groupby,但得到了想要的结果,但是它获得了另一个表中最旧的记录。有谁知道我怎么能做到这一点?

我的第一个查询

SELECT users.name, users.email, users.phone, users.parent_id, users.section_id, finances.amount, finances.description, schoolyears.name, finances.date from users JOIN finances on users.id = finances.user_id JOIN schoolyears on users.school_id = schoolyears.school_id ORDER BY finances.date DESC; 结果我得到了

+-----------------+------------------------------+--------------------+-----------+------------+--------+--------------+--------------+------------+ | name | email | phone | parent_id | section_id | amount | description | name | date | +-----------------+------------------------------+--------------------+-----------+------------+--------+--------------+--------------+------------+ | Madelynn Stokes | moore.dominic@cartwright.com | +63 (971) 659-8143 | 10 | NULL | 1000 | New Payables | SY-2019-2020 | 2019-11-14 | | Annamarie Morar | emile99@hotmail.com | (0997) 212-7919 | 3 | NULL | 500 | New Pays | SY-2019-2020 | 2019-11-14 | | Madelynn Stokes | moore.dominic@cartwright.com | +63 (971) 659-8143 | 10 | NULL | 5000 | Old Payables | SY-2019-2020 | 2019-11-13 | | Annamarie Morar | emile99@hotmail.com | (0997) 212-7919 | 3 | NULL | 200 | Old Pays | SY-2019-2020 | 2019-11-13 | +-----------------+------------------------------+--------------------+-----------+------------+--------+--------------+--------------+------------+ 我只想要其他表中的最新记录。新应付款和新工资。

我尝试了第二个查询

SELECT users.name, users.email, users.phone, users.parent_id, users.section_id, finances.amount, finances.description, schoolyears.name, finances.date from users JOIN finances on users.id = finances.user_id JOIN schoolyears on users.school_id = schoolyears.school_id GROUP BY users.id ORDER BY finances.date DESC; 它有效,但是我得到了最早的记录。

+-----------------+------------------------------+--------------------+-----------+------------+--------+--------------+--------------+------------+ | name | email | phone | parent_id | section_id | amount | description | name | date | +-----------------+------------------------------+--------------------+-----------+------------+--------+--------------+--------------+------------+ | Madelynn Stokes | moore.dominic@cartwright.com | +63 (971) 659-8143 | 10 | NULL | 5000 | Old Payables | SY-2019-2020 | 2019-11-13 | | Annamarie Morar | emile99@hotmail.com | (0997) 212-7919 | 3 | NULL | 200 | Old Pays | SY-2019-2020 | 2019-11-13 | +-----------------+------------------------------+--------------------+-----------+------------+--------+--------------+--------------+------------+

问题来源于stack overflow

展开
收起
保持可爱mmm 2019-11-14 18:05:40 323 0
1 条回答
写回答
取消 提交回答
  • 您可以在下面尝试-

    SELECT users.name, users.email, users.phone, users.parent_id, users.section_id, finances.amount, finances.description, schoolyears.name, finances.dateval from users JOIN finances on users.id = finances.user_id JOIN schoolyears on users.school_id = schoolyears.school_id where finances.dateval= (select max(dateval) from finances f where finances.user_id=f.user_id)

    2019-11-14 18:06:32
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载