Wednesday, April 29, 2015

How to group by DESC order

You do group by, then you want to get the latest/smallest value. see below example

Table name `log`
idnamedate
1mia2015-04-10
2mia2015-04-03
3jame2015-04-11
4jame2015-04-13
5jame2015-04-01
6jame2015-04-15


select * from `log` group by name return below
idnamedate
1mia2015-04-10
2jame2015-04-11


select id,name,min(date) from `log` group by name return below
idnamedate
1mia2015-04-03
2jame2015-04-01


Another example: SELECT max(id) as id, asker FROM questions GROUP by asker

Tag: Give priority to ORDER BY over a GROUP BY in MySQL without subquery, SQL query ordering by alternating group ID

No comments: