表名:table
id uid username message dateline
1 6 a 111 1284240714(时间戳)
2 6 a 222 1268840565
3 8 b 444 1266724527
4 9 c 555 1266723391
5 9 c 666 1555555555
我想查询所有字段,去除uid重复的行,只保留id值最大的一条
查询结果是
id uid username message dateline
2 6 a 222 1268840565
3 8 b 444 1266724527
5 9 c 666 1555555555
怎么实现?
SELECT * FROM TABLE t WHERE t.id IN (
SELECT MAX(id) FROM TABLE GROUP BY uid;
)
还有没有更高效一点的方法
回答
SELECT * FROM TABLE t WHERE EXISTS (
SELECT 1 FROM TABLE GROUP BY uid
HAVING MAX(id) = t.id
)