`id` int(11) NOT NULL AUTO_INCREMENT,
`title` text,
`times` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
)
mysql>select * from testgroup+------+---------------------+
| id | time|
+------+---------------------+
|1 | 2012-02-28 01:20:44 |
|2 | 2012-02-28 01:20:47 |
|3 | 2012-02-28 01:20:50 |
| 53 | 2012-02-28 01:21:08 |
| 95 | 2012-02-28 01:21:09 |
| 18 | 2012-02-28 01:21:09 |
|4 | 2012-02-28 01:21:10 |
| 65 | 2012-02-28 01:21:10 |
| 15 | 2012-02-28 01:21:11 |
| 79 | 2012-02-28 01:21:12 |
| 51 | 2012-02-28 01:21:12 |
| 17 | 2012-02-28 01:21:12 |
|1 | 2012-02-28 01:21:26 |
|2 | 2012-02-28 01:21:30 |
|3 | 2012-02-28 01:21:36 |
|3 | 2012-02-28 01:21:38 |
+------+---------------------+
16 rows in set (0.00 sec)
mysql>select * from testgroup t1 where (t1.id,t1.time) in(select t2.id,max(t2.time) from testgroup t2 group by t2.id)
+------+---------------------+
| id | time|
+------+---------------------+
| 53 | 2012-02-28 01:21:08 |
| 95 | 2012-02-28 01:21:09 |
| 18 | 2012-02-28 01:21:09 |
|4 | 2012-02-28 01:21:10 |
| 65 | 2012-02-28 01:21:10 |
| 15 | 2012-02-28 01:21:11 |
| 79 | 2012-02-28 01:21:12 |
| 51 | 2012-02-28 01:21:12 |
| 17 | 2012-02-28 01:21:12 |
|1 | 2012-02-28 01:21:26 |
|2 | 2012-02-28 01:21:30 |
|3 | 2012-02-28 01:21:38 |
+------+---------------------+
12 rows in set (0.04 sec)
----------------------------------------风骚的分割线--------------------------------------------
我这边time字段是datetime类型,你可以先将你time字段做类型转换处理,str_to_date(str,'%Y-%m-%d')即可
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)