下面的SELECT语句如何使查询GROUP BY X DESC
select *
from messages
where receiverID = '5'
group BY senderID
order by id DESC
数据库:
id | senderID | receiverID | message
1 | 245 | 5 | test 1
2 | 89 | 5 | test 2
3 | 79 | 5 | test 3
4 | 245 | 5 | test 4
5 | 245 | 5 | test 5
对于senderID = 245我预计到id = 5返回该行,但它dosent它返回id = 1的行,但我想要最后一行。如何实现这一目标?
回报:
id | senderID | receiverID | message
1 | 245 | 5 | test 1
2 | 89 | 5 | test 2
3 | 79 | 5 | test 3
喔我做到了:d
所以这是工作,对任何人有类似问题的代码
SELECT * FROM (SELECT * FROM messages WHERE
receiverID = '5' ORDER BY id DESC) AS m GROUP BY senderID ORDER BY id DESC
你能不能改一下这个问题吗?也许你试图实现的一个例子列表结果 – 2012-02-16 10:12:27
为什么你在这里使用'GROUP BY'? – piotrm 2012-02-16 10:20:42
我使用GROUP BY,因为我想要每个senderID的最后一行。 – Ben 2012-02-16 10:25:20