我是mysql查询的初学者我有这样的表行和值在此处输入图像描述我想从表中选择:按id降序排序和按电话分组,所以结果如下在此处输入图像描述请任何人帮帮我。。我已经把这个放进去了选择*from messages where 1 group by phone order by id desc但这是错误的谢谢您
ut6juiuv1#
它将不适用于group by而不是try below查询
SELECT text,phone FROM @tblPhone tmp WHERE id = (SELECT MAX(Id) FROM @tblPhone tmp1 WHERE tmp1.PHONE = tmp.Phone) ORDER BY Id desc
qqrboqgw2#
要获取每个电话的最新消息属性,可以使用self-join
select a.* from messages a join ( select phone, max(id) id from messages group by phone ) b on a.phone = b.phone and a.id = b.id
或者使用左连接
select a.* from messages a left join messages b on a.phone = b.phone and a.id < b.id where b.phone is null
oaxa6hgo3#
请尝试下面的查询
select phone, text from messages group by phone order by id DESC
3条答案
按热度按时间ut6juiuv1#
它将不适用于group by而不是try below查询
qqrboqgw2#
要获取每个电话的最新消息属性,可以使用self-join
或者使用左连接
oaxa6hgo3#
请尝试下面的查询