There is a table messages that contains data as shown below:
Id Name Other_Columns ------------------------- 1 A A_data_1 2 A A_data_2 3 A A_data_3 4 B B_data_1 5 B B_data_2 6 C C_data_1
If I run a query select * from messages group by name, I will get the result as:
1 A A_data_1 4 B B_data_1 6 C C_data_1
什么查询将返回以下结果?
3 A A_data_3 5 B B_data_2 6 C C_data_1
也就是说,应该返回每组中的最后一条记录。
目前,这是我使用的查询:
SELECT * FROM (SELECT * FROM messages ORDER BY id DESC) AS x GROUP BY name
但这看起来效率很低。还有其他方法可以达到相同的结果吗?
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号