日期:2014-05-16 浏览次数:21102 次
SELECT * FROM (SELECT `id`, `reply_id`, `name`, `content`, `time` FROM `reply` ORDER BY `time` DESC) tbl GROUP BY `reply_id` ORDER BY `time` DESC
------解决方案--------------------
其中的一种解法如下。
mysql> select * from reply;
+----+----------+-------+---------+------+
| id | reply_id | name | content | time |
+----+----------+-------+---------+------+
| 1 | 1 | test1 | test1 | 1 |
| 2 | 1 | test2 | test2 | 3 |
| 3 | 2 | test3 | test3 | 2 |
| 4 | 2 | test4 | test4 | 4 |
| 5 | 3 | test5 | test5 | 1 |
| 6 | 3 | test6 | test6 | 2 |
| 7 | 3 | test7 | test7 | 5 |
+----+----------+-------+---------+------+
7 rows in set (0.00 sec)
mysql> select * from reply r
-> where not exists (select 1 from reply where reply_id=r.reply_id and time>r.time)
-> order by time desc;
+----+----------+-------+---------+------+
| id | reply_id | name | content | time |
+----+----------+-------+---------+------+
| 7 | 3 | test7 | test7 | 5 |
| 4 | 2 | test4 | test4 | 4 |
| 2 | 1 | test2 | test2 | 3 |
+----+----------+-------+---------+------+
3 rows in set (0.00 sec)
mysql>