MySQL 5.7组按最新记录

时间:2017-07-17 16:18:55

标签: mysql

非常简单的选择,但我很困惑:

create table users (id int, type int);
insert into users values(1, 100), (2, 101), (3, 100);
mysql> select * from users;                                                                                                               
+------+------+                                                                                                                           
| id   | type |                                                                                                                                                                                                   
+------+------+                                                                                                                                                                                                   
|    1 |  100 |                                                                                                                                                                                                   
|    2 |  101 |                                                                                                                                                                                                   
|    3 |  100 |                                                                                                                                                                                                   
+------+------+

我想得到结果:

+------+------+
| id   | type |
+------+------+
|    3 |  100 |
|    2 |  101 |
+------+------+

我的查询是:

MySQL 5.7版返回:

mysql> select * from (select * from users order by id desc) as t group by type;
+------+------+
| id   | type |
+------+------+
|    1 |  100 |
|    2 |  101 |
+------+------+

在MySQL 5.5上它按预期工作。 sql_mode为NULL

感谢您的回复。扩展表有更清晰的结果:

create table users (id int, type int, level int);
insert into users values(1, 100, 1000), (2, 101, 1001), (3, 100, 1002);

mysql> select max(id), type, level from users group by type;
+---------+------+-------+
| max(id) | type | level |
+---------+------+-------+
|       3 |  100 |  1000 | <- expected 1002
|       2 |  101 |  1001 |
+---------+------+-------+

这有效:

mysql> SELECT t1.* FROM users t1 INNER JOIN (SELECT type, MAX(id) AS max_id FROM users GROUP BY type) t2 ON t1.type = t2.type AND t1.id = t2.max_id ORDER BY id DESC;
+------+------+-------+
| id   | type | level |
+------+------+-------+
|    3 |  100 |  1002 |
|    2 |  101 |  1001 |
+------+------+-------+

3 个答案:

答案 0 :(得分:2)

您应该使用聚合功能和分组

select max(id), type
from users 
group by type;
order by id desc

mysql5.5中的结果不是基于group by是偶然的..(这个beahvior在sql中不推荐使用,在5.7中的默认sql_mode中不允许)

答案 1 :(得分:1)

为了完整性,我可以提供以下查询,如果您想要返回具有任意列数的整个记录​​,该查询将起作用。

SELECT t1.*
FROM users t1
INNER JOIN
(
    SELECT type, MAX(id) AS max_id
    FROM users
    GROUP BY type
) t2
    ON t1.type = t2.type AND
       t1.id   = t2.max_id

答案 2 :(得分:0)

非常少的SQL方言(甚至只有MySQL)支持

GROUP BY without aggregation;事实上,它在5.5中的预期工作(大部分)只是方便,不是由MySQL保证。正式地说,它从非分组的非聚合字段中获取有效的随机值(从组的行中遇到)。