mysql获得排序依据之前发生的分组依据
作者:互联网
我有三个表格,在这里我只向您显示相关的列
Table: groups Columns: group_id, name.
Table: groups_to_message Columns:
group_id, message_idTable: messages Columns: message_id,
created (date)
我基本上需要找到每个组的最后一条消息,而不必显示重复的组.
我试过像这样使用group by:
SELECT m.created, g.group_id
FROM groupss as g
JOIN group_to_message as gm ON (g.group_id = gm.group_id)
JOIN messages as m
GROUP BY g.group_id
ORDER BY m.created DESC
这将导致成功分组,但是在ORDER BY之前完成,因此第一个结果在排序之前.
任何帮助表示赞赏.
解决方法:
如果我没看错您的问题,请使用MAX().这应该为您提供每个组的最新商品
例…
SELECT MAX(m.created), g.group_id
FROM groupss as g
JOIN group_to_message as gm ON (g.group_id = gm.group_id)
JOIN messages as m ON (gm.message_id = gm.message_id)
GROUP BY g.group_id
ORDER BY m.created DESC
标签:sql-order-by,sql,mysql,group-by 来源: https://codeday.me/bug/20191209/2095655.html