34,575
社区成员
发帖
与我相关
我的任务
分享
CREATE TABLE `a` (
`id` int(10) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`addtime` int(16) DEFAULT NULL
)
select * from
(select * from a ORDER BY addtime) aa
GROUP BY NAME
-- 不建议使用非标准语法,尽 管 mysql 支持
mysql> CREATE TABLE `a` (
-> `id` int(10) NOT NULL,
-> `name` varchar(255) DEFAULT NULL,
-> `addtime` int(16) DEFAULT NULL
-> );
Query OK, 0 rows affected (0.02 sec)
mysql>
mysql> insert into a values(1 , 'aa', 1482823400);
Query OK, 1 row affected (0.00 sec)
mysql> insert into a values(10, 'bb', 1482823600);
Query OK, 1 row affected (0.00 sec)
mysql> insert into a values(4 , 'bb', 1482823200);
Query OK, 1 row affected (0.00 sec)
mysql> insert into a values(3 , 'cc', 1482823700);
Query OK, 1 row affected (0.00 sec)
mysql> insert into a values(5 , 'cc', 1482823300);
Query OK, 1 row affected (0.00 sec)
mysql> insert into a values(6 , 'ff', 1482823800);
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> select * from a
-> where not exists(
-> select * from a x where a.name = x.name and a.addtime > x.addtime);
+----+------+------------+
| id | name | addtime |
+----+------+------------+
| 1 | aa | 1482823400 |
| 4 | bb | 1482823200 |
| 5 | cc | 1482823300 |
| 6 | ff | 1482823800 |
+----+------+------------+
4 rows in set (0.02 sec)
mysql>
mysql> drop table a;
Query OK, 0 rows affected (0.00 sec)