MySQL选择组中的最大记录

时间:2021-10-14 12:45:01

I am trying to create a query in a table that has some 500,000 records and some 50 or 60 columns. What I need is to collate these records into groups and select the max record in each group.

我试图在一个有500,000条记录和50或60列的表中创建一个查询。我需要的是将这些记录整理成组并选择每组中的最大记录。

To simplify the problem I have a table as follows

为简化问题,我有一个表格如下

+----+-------------+----------+--------+
| id | external_id | group_id | mypath |
+----+-------------+----------+--------+
|  1 |        1003 |        1 | a      | 
|  2 |        1004 |        2 | b      | 
|  3 |        1005 |        2 | c      | 
+----+-------------+----------+--------+

The simple group by is as follows

简单组如下

select * from temp GROUP BY group_id

which returns

返回

+----+-------------+----------+--------+
| id | external_id | group_id | mypath |
+----+-------------+----------+--------+
|  1 |        1003 |        1 | a      | 
|  2 |        1004 |        2 | b      | 
+----+-------------+----------+--------+

Nice but not what I want. What I want is the entire record for max enternal_id in each group. In other words

很好,但不是我想要的。我想要的是每组中max enternal_id的完整记录。换一种说法

+----+-------------+----------+--------+
| id | external_id | group_id | mypath |
+----+-------------+----------+--------+
|  1 |        1003 |        1 | a      | 
|  3 |        1005 |        2 | c      | 
+----+-------------+----------+--------+

Somehow I am looking to put a max(external_id) statement in here to filter what is needed but so far all my investigation has failed. Some guidance would be appreciated. It is important that when returning the max(external_id) that the entire record is selected as the path column differs.

不知怎的,我想在这里放一个max(external_id)语句来过滤所需要的但到目前为止我所有的调查都失败了。一些指导意见将不胜感激。重要的是,当返回max(external_id)时,选择整个记录作为路径列的不同。

1 个解决方案

#1


19  

Much info at http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/

有关这些信息,请访问http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/

This has always been an annoying issue in MySQL. There have been ways around it, such as concatenating several fields together (starting with external_id), and then selecting the MAX() of that, and then breaking it back apart.

这在MySQL中一直是个烦人的问题。它有很多方法,例如将几个字段连接在一起(从external_id开始),然后选择它的MAX(),然后将它分开。

I suggest you use a derived table. First table (t1) is derived from a simple query where you identify the MAX(external_id), then you join from that to get the rest of the data.

我建议你使用派生表。第一个表(t1)派生自一个简单的查询,您可以在其中识别MAX(external_id),然后从中加入以获取其余数据。

THIS IS ONLY IF external_id IS UNIQUE

这只是在external_id是独一无二的时候

SELECT 
   t1.group_id, some_table.id, some_table.mypath
FROM 
   (
      SELECT group_id, MAX(external_id) AS external_id
      FROM some_table
      GROUP BY group_id
   ) as t1
INNER JOIN 
   sometable ON t1.external_id = sometable.external_id
WHERE ...

#1


19  

Much info at http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/

有关这些信息,请访问http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/

This has always been an annoying issue in MySQL. There have been ways around it, such as concatenating several fields together (starting with external_id), and then selecting the MAX() of that, and then breaking it back apart.

这在MySQL中一直是个烦人的问题。它有很多方法,例如将几个字段连接在一起(从external_id开始),然后选择它的MAX(),然后将它分开。

I suggest you use a derived table. First table (t1) is derived from a simple query where you identify the MAX(external_id), then you join from that to get the rest of the data.

我建议你使用派生表。第一个表(t1)派生自一个简单的查询,您可以在其中识别MAX(external_id),然后从中加入以获取其余数据。

THIS IS ONLY IF external_id IS UNIQUE

这只是在external_id是独一无二的时候

SELECT 
   t1.group_id, some_table.id, some_table.mypath
FROM 
   (
      SELECT group_id, MAX(external_id) AS external_id
      FROM some_table
      GROUP BY group_id
   ) as t1
INNER JOIN 
   sometable ON t1.external_id = sometable.external_id
WHERE ...