With Data sheet comments
------------------------------------------------
| ID | NewsID | Comment | Thetime |
------------------------------------------------
| 1 | 1 | AAA | 11 |
------------------------------------------------
| 2 | 1 | BBB | 12 |
------------------------------------------------
| 3 | 2 | CCC | 12 |
------------------------------------------------
NewSID is the news ID, there are several comments per news comment,thetime is the time to comment
Now you want to see the latest comment for each news article:
SELECT * from Comments GROUP BY NewsID obviously not.
SELECT * from Comments GROUP by NewsID ORDER BY thetime Desc is an out-of-group sort or not
There are two ways to accomplish this:
(1)
Selet Tt.id,tt.newsid,tt.comment,tt.thetime from (
Select Id,newsid,comment,thetime from comments ORDER BY thetime Desc) as TT GROUP by NewsID
(2)
Select Id,newsid,comment,thetime from comments as TT GROUP BY Id,newsid,comment,thetime have
Thetime= (select Max (thetime) from comments where Newsid=tt.newsid)
Transferred from: http://blog.csdn.net/shellching/article/details/8292338
MySQL GROUP by group within sort