如何为所有用户选择所有最新记录

时间:2017-10-17 14:08:39

标签: mysql sql

我有一个约会表,我需要为所有用户选择最新的记录,目前我的查询返回最旧的每个用户ID而不是最新的每个用户

约会表

+-------+--------------+--------------+--------------+
| ID    | time         | userid       | description  |
+-------+--------------+--------------+--------------+
| 1     | 2017-10-20   | 4            | etc          |
+-------+--------------+--------------+--------------+
| 2     | 2017-10-21   | 6            | etc          |
+-------+--------------+--------------+--------------+
| 3     | 2017-10-22   | 7            | etc          |
+-------+--------------+--------------+--------------+
| 4     | 2017-10-23   | 8            | etc          |
+-------+--------------+--------------+--------------+
| 5     | 2017-10-24   | 6            | etc          |
+-------+--------------+--------------+--------------+
| 6     | 2017-10-25   | 7            | etc          |
+-------+--------------+--------------+--------------+

用户表

+-------+--------------+--------------+--------------+
| ID    | first        | last         | status       |
+-------+--------------+--------------+--------------+
| 4     | jo           | do           | 1            |
+-------+--------------+--------------+--------------+
| 6     | jid          | did          | 1            |
+-------+--------------+--------------+--------------+
| 7     | jone         | done         | 1            |
+-------+--------------+--------------+--------------+
| 8     | ja           | da           | 1            |
+-------+--------------+--------------+--------------+

当前查询

$sql = "SELECT *
        FROM appointment 
        LEFT JOIN users AS user
        ON user.id = appointment.userid
        WHERE user.status = 1
        GROUP BY appointment.userid
        ";

当前结果

+-------+--------------+--------------+--------------+
| ID    | time         | userid       | description  |
+-------+--------------+--------------+--------------+
| 1     | 2017-10-20   | 4            | etc          |
+-------+--------------+--------------+--------------+
| 2     | 2017-10-21   | 6            | etc          |
+-------+--------------+--------------+--------------+
| 3     | 2017-10-22   | 7            | etc          |
+-------+--------------+--------------+--------------+
| 4     | 2017-10-23   | 8            | etc          |
+-------+--------------+--------------+--------------+

预期输出

+-------+--------------+--------------+--------------+
| ID    | time         | userid       | description  |
+-------+--------------+--------------+--------------+
| 1     | 2017-10-20   | 4            | etc          |
+-------+--------------+--------------+--------------+
| 4     | 2017-10-23   | 8            | etc          |
+-------+--------------+--------------+--------------+
| 5     | 2017-10-24   | 6            | etc          |
+-------+--------------+--------------+--------------+
| 6     | 2017-10-25   | 7            | etc          |
+-------+--------------+--------------+--------------+

5 个答案:

答案 0 :(得分:1)

试试这个

SELECT a.ID, a.time, a.userid, a.description 
FROM users u
INNER JOIN appointment a ON u.id = a.userid
WHERE u.status = 1
AND time in(SELECT MAX(time) from appointment t WHERE t.userid = a.userid )

答案 1 :(得分:0)

您可以进行自我加入以确定最新约会:

SELECT s.* FROM (
    SELECT *
    FROM appointment 
    LEFT JOIN users AS user
     ON user.id = appointment.userid and user.status = 1) s
LEFT JOIN appointment p
 ON(p.userid = s.userid and p.time < s.time)
WHERE p.time IS NULL

虽然我不明白为什么LEFT JOINusers表。

答案 2 :(得分:0)

SELECT MAX(a.ID) ID, MAX(a.time) time, a.userid, MAX(a.description) description
FROM appointment a
INNER JOIN users u ON u.id = a.userid
WHERE u.status = 1
GROUP BY a.userid
ORDER BY a.ID

输出

ID time                 userid description
1  2017-10-20T00:00:00Z 4      etc
5  2017-10-24T00:00:00Z 6      etc
6  2017-10-25T00:00:00Z 7      etc
4  2017-10-23T00:00:00Z 8      etc

SQL小提琴:http://sqlfiddle.com/#!9/6b0c1c/7/0

答案 3 :(得分:0)

我认为带有用户表的left join是不必要的。 以下查询将给出预期结果

select a1.* from appointment a1
inner join (select max(id) as id ,userid from appointment group by userid) a2
on a1.id=a2.id

SQL Fiddle

答案 4 :(得分:-1)

SELECT max(appointment.id), max(time), user.id, description
FROM appointment 
LEFT JOIN users AS user
ON user.id = appointment.userid
WHERE user.status = 1
GROUP BY appointment.userid