//Multi-table query: Get the latest data before you execute a multi-table query
SELECT * from' Students ' st ' Right JOIN( SELECT * from
(
SELECT * fromGoutongWHEREgoutongs= ' asdf 'ORDER byTimeDESC
) asGttGROUP bygtt.name_idORDER byGtt.goutong_timeDESC) GT
on' GT '. ' name_id '=' St '. ' ID ' LIMITTen
//Sort the query by time and then group (group by)
SELECT * from(SELECT * fromGoutongWHEREGoutongs='asdf' ORDER byTimeDESC) asGttGROUP bygtt.name_idORDER byGtt.timeDESC
Reference: http://blog.csdn.net/shellching/article/details/8292338
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)
MySQL multi-table query and its group by group sort