sql获取所有消息

时间:2012-06-01 04:49:14

标签: mysql sql

我已经实现了这个sql语句:

SELECT users.firstname, users.lastname, users.screenname, profile.profile_pic,
users.user_id, messages.msgid, messages.message, messages.action,
DATE_FORMAT(messages.date_sent, '%M %d, %Y %r') AS date_sent
FROM messages messages
JOIN users ON messages.sent_by = users.user_id
LEFT JOIN profile ON (users.user_id = profile.user_id)
WHERE messages.recepient_id = "12052821" and messages.action = false
ORDER BY msgid DESC

它输出正确的输出(如下) enter image description here

问题是你如何整理所有给你发信息的用户列表,并分别显示msgid DESC的前1条消息ORDER BY ? (不得重复用户)

以下是我的表格 enter image description here

我猜我上面的查询需要一点点增强。提前谢谢你们

2 个答案:

答案 0 :(得分:0)

解决了问题 - 只需在

订购之前添加此行:GROUP BY users.user_id

答案 1 :(得分:-1)

尝试

ORDER BY msgid DESC LIMIT 1