检索每一个组中的最后一条记录-MySQL - Retrieving the last record in each group - MySQL

问题:

There is a table messages that contains data as shown below: 有一个表messages ,其中包含数据,以下所示: this

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: 若是运行查询,请select * from messages group by name ,结果将是: spa

1    A       A_data_1
4    B       B_data_1
6    C       C_data_1

What query will return the following result? 什么查询将返回如下结果? .net

3    A       A_data_3
5    B       B_data_2
6    C       C_data_1

That is, the last record in each group should be returned. 即,应返回每一个组中的最后一条记录。 code

At present, this is the query that I use: 目前,这是我使用的查询: ci

SELECT
  *
FROM (SELECT
  *
FROM messages
ORDER BY id DESC) AS x
GROUP BY name

But this looks highly inefficient. 但这看起来效率很低。 Any other ways to achieve the same result? 还有其余方法能够达到相同的结果吗? get


解决方案:

参考一: https://stackoom.com/question/5VbM/检索每一个组中的最后一条记录-MySQL
参考二: https://oldbug.net/q/5VbM/Retrieving-the-last-record-in-each-group-MySQL
相关文章
相关标签/搜索