独特但最新的行
Unique but the latest rows
我正在使用 mysql 5.7 服务器,我有以下 table,其中 id
作为主键。
我的Table
mysql> select * from abc;
+----+---------+-------+
| id | name | place |
+----+---------+-------+
| 1 | asd1 | abcd1 |
| 2 | asd2 | abcd1 |
| 3 | asd1 | abcd2 |
| 4 | asd3 | abcd1 |
| 5 | asd4 | abcd1 |
| 6 | asd1 | abcd1 |
| 7 | asd2 | abcd2 |
| 8 | asd3 | abcd2 |
| 9 | asd4 | abcd2 |
| 10 | asd3 | abcd1 |
| 11 | asd4 | abcd1 |
| 12 | asd4 | abcd2 |
| 13 | asd1 | abcd2 |
+----+---------+-------+
13 rows in set (0.00 sec)
预期结果
我想 select 具有唯一 name
的行具有最新的 id
。
换句话说,我的预期结果应该如下:
+----+---------+-------+
| id | name | place |
+----+---------+-------+
| 13 | asd1 | abcd2 |
| 7 | asd2 | abcd2 |
| 10 | asd3 | abcd1 |
| 12 | asd4 | abcd2 |
+----+---------+-------+
4 rows in set (0.00 sec)
我试过的
mysql> select * from abc group by name order by id desc;
+----+------+-------+
| id | name | place |
+----+------+-------+
| 5 | asd4 | abcd1 |
| 4 | asd3 | abcd1 |
| 2 | asd2 | abcd1 |
| 1 | asd1 | abcd1 |
+----+------+-------+
4 rows in set (0.00 sec)
mysql> select * from (select * from abc as t order by t.id desc) as st1 group by t1.orderID;
+----+------+-------+
| id | name | place |
+----+------+-------+
| 1 | asd1 | abcd1 |
| 2 | asd2 | abcd1 |
| 4 | asd3 | abcd1 |
| 5 | asd4 | abcd1 |
+----+------+-------+
4 rows in set (0.00 sec)
mysql> select * from abc l inner join (select * from abc group by name) r on l.id = r.id;
+----+------+-------+----+------+-------+
| id | name | place | id | name | place |
+----+------+-------+----+------+-------+
| 1 | asd1 | abcd1 | 1 | asd1 | abcd1 |
| 2 | asd2 | abcd1 | 2 | asd2 | abcd1 |
| 4 | asd3 | abcd1 | 4 | asd3 | abcd1 |
| 5 | asd4 | abcd1 | 5 | asd4 | abcd1 |
+----+------+-------+----+------+-------+
4 rows in set (0.01 sec)
问题
获得预期结果的正确SQL是什么?准确简单者优先
下面需要吗-
SELECT MAX(id), name
FROM abc
GROUP BY name
您可以使用相关子查询过滤每组的顶部记录:
select a.*
from abc a
where a.id = (select max(a1.id) from abc a1 where a1.name = a.name)
order by a.name
为了提高此查询的性能,您需要在 (name, id)
上建立索引。
SELECT t1.*
FROM abc t1
NATURAL JOIN ( SELECT MAX(id) id, name
FROM abс
GROUP BY name ) t2
我正在使用 mysql 5.7 服务器,我有以下 table,其中 id
作为主键。
我的Table
mysql> select * from abc;
+----+---------+-------+
| id | name | place |
+----+---------+-------+
| 1 | asd1 | abcd1 |
| 2 | asd2 | abcd1 |
| 3 | asd1 | abcd2 |
| 4 | asd3 | abcd1 |
| 5 | asd4 | abcd1 |
| 6 | asd1 | abcd1 |
| 7 | asd2 | abcd2 |
| 8 | asd3 | abcd2 |
| 9 | asd4 | abcd2 |
| 10 | asd3 | abcd1 |
| 11 | asd4 | abcd1 |
| 12 | asd4 | abcd2 |
| 13 | asd1 | abcd2 |
+----+---------+-------+
13 rows in set (0.00 sec)
预期结果
我想 select 具有唯一 name
的行具有最新的 id
。
换句话说,我的预期结果应该如下:
+----+---------+-------+
| id | name | place |
+----+---------+-------+
| 13 | asd1 | abcd2 |
| 7 | asd2 | abcd2 |
| 10 | asd3 | abcd1 |
| 12 | asd4 | abcd2 |
+----+---------+-------+
4 rows in set (0.00 sec)
我试过的
mysql> select * from abc group by name order by id desc;
+----+------+-------+
| id | name | place |
+----+------+-------+
| 5 | asd4 | abcd1 |
| 4 | asd3 | abcd1 |
| 2 | asd2 | abcd1 |
| 1 | asd1 | abcd1 |
+----+------+-------+
4 rows in set (0.00 sec)
mysql> select * from (select * from abc as t order by t.id desc) as st1 group by t1.orderID;
+----+------+-------+
| id | name | place |
+----+------+-------+
| 1 | asd1 | abcd1 |
| 2 | asd2 | abcd1 |
| 4 | asd3 | abcd1 |
| 5 | asd4 | abcd1 |
+----+------+-------+
4 rows in set (0.00 sec)
mysql> select * from abc l inner join (select * from abc group by name) r on l.id = r.id;
+----+------+-------+----+------+-------+
| id | name | place | id | name | place |
+----+------+-------+----+------+-------+
| 1 | asd1 | abcd1 | 1 | asd1 | abcd1 |
| 2 | asd2 | abcd1 | 2 | asd2 | abcd1 |
| 4 | asd3 | abcd1 | 4 | asd3 | abcd1 |
| 5 | asd4 | abcd1 | 5 | asd4 | abcd1 |
+----+------+-------+----+------+-------+
4 rows in set (0.01 sec)
问题
获得预期结果的正确SQL是什么?准确简单者优先
下面需要吗-
SELECT MAX(id), name
FROM abc
GROUP BY name
您可以使用相关子查询过滤每组的顶部记录:
select a.*
from abc a
where a.id = (select max(a1.id) from abc a1 where a1.name = a.name)
order by a.name
为了提高此查询的性能,您需要在 (name, id)
上建立索引。
SELECT t1.*
FROM abc t1
NATURAL JOIN ( SELECT MAX(id) id, name
FROM abс
GROUP BY name ) t2