0
我有两个SQL表,其中一个有用户,另一个有消息。返回来自分组邮件的最后一条消息SQL
现在我的查询是:
SELECT messages.*, username FROM messages
JOIN users ON messages.from_ID = users.user_ID
WHERE to_ID = 19
GROUP BY thread_ID
ORDER BY msg_ID DESC;
实体(消息):
msg_ID
from_ID
to_ID
subject
message
date
眼下,它返回FIRST消息,其中to_ID = 19
。我想要做的是显示最新的消息,由最高的msg_ID
排序。我可以对它做一个order by
,但是我的sql语句只返回一行,所以我必须在它到达group by
声明之前做一些事情。
如果这些是两行有资格被退回:
msg_ID from_ID to_ID subject message date thread_id username
15 26 19 Hey testing string2 1477750565 1 testing
17 26 19 Hey testing string. 1477750594 1 testing
我现在的结果是:
msg_ID from_ID to_ID subject message date thread_id username
15 26 19 Hey testing string2 1477750565 1 testing
我想要什么:
msg_ID from_ID to_ID subject message date thread_id username
17 26 19 Hey testing string. 1477750594 1 testing // msg_ID is higher here