GROUP BY how to keep repeating the second line instead of the default is the fir

I would like to ask you greatly, in the query table practice, if you need to repeat, but retained second, removal of the first row, the query should be how to write? For example:
Data in table a
id name age paixu
1 Zhang San 185
2 Li Si 193
3 Wang Wu 304
4 Shuai Shuai 161
5 Niu Niu 162

For example, according to the field age query age does not duplicate records, sorted by the value of the paixu field, usually with a statement:
SELECT * FROM a where age != 0 GROUP BY age ORDER BY paixu
Results:
id name age paixu
4 Shuai Shuai 161
2 Li Si 193
3 Wang Wu 304
1 Zhang San 185


The above results excluded ID 5 line but if you need to keep ID 5 line and remove ID 4 line? Such results need to be for:

id name age paixu
5 Niu Niu 162
2 Li Si 193
3 Wang Wu 304
1 Zhang San 185


The query should be how to write it?

Started by Byron at November 01, 2016 - 10:06 PM

Reference resources:

SELECT m1.*
FROM messages m1 LEFT JOIN messages m2
 ON (m1.name = m2.name AND m1.id <m2.id)
WHERE m2.id IS NULL;

Posted by Joanna at November 07, 2016 - 10:18 PM

SELECT * FROM (select * from a order by id desc) t where age != 0 GROUP BY age ORDER BY paixu

Posted by Beatrix at November 19, 2016 - 11:02 PM