我想得到每个用户和他们最后的付款。我这里有两张桌子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 | [email protected] | +63 (971) 659-8143 | 10 | NULL | 1000 | New Payables | SY-2019-2020 | 2019-11-14 |
| Annamarie Morar | [email protected] | (0997) 212-7919 | 3 | NULL | 500 | New Pays | SY-2019-2020 | 2019-11-14 |
| Madelynn Stokes | [email protected] | +63 (971) 659-8143 | 10 | NULL | 5000 | Old Payables | SY-2019-2020 | 2019-11-13 |
| Annamarie Morar | [email protected] | (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 | [email protected] | +63 (971) 659-8143 | 10 | NULL | 5000 | Old Payables | SY-2019-2020 | 2019-11-13 |
| Annamarie Morar | [email protected] | (0997) 212-7919 | 3 | NULL | 200 | Old Pays | SY-2019-2020 | 2019-11-13 |
+-----------------+------------------------------+--------------------+-----------+------------+--------+--------------+--------------+------------+
最佳答案
你可以在下面试试-
DEMO
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)
关于mysql - MySQL按日期选择顺序返回重复值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58851942/