MySQL select 按日期排序 returns 重复值

MySQL select orderby date returns duplicate values

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

我的第一个查询

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 |
+-----------------+------------------------------+--------------------+-----------+------------+--------+--------------+--------------+------------+

我只想要其他 table 中的最新记录。新应付款和新付款。

我尝试了第二个查询

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 |
+-----------------+------------------------------+--------------------+-----------+------------+--------+--------------+--------------+------------+

你可以试试下面的-

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)