Group by and order by mysql

Viewed 46
SELECT * FROM `jt_model_files` ORDER by id DESC

enter image description here

SELECT * FROM `jt_model_files` GROUP BY file_id ORDER by id DESC

enter image description here

What i want to achieve is

enter image description here

What is the possible solution.

1 Answers

SELECT * with GROUP BY is always problematic, even if it is allowed, because the results are undefined.
For your sample data, you should GROUP BY model_id, file_id and get the max value of id:

SELECT MAX(id) AS id, model_id, file_id
FROM jt_model_files
GROUP BY model_id, file_id
ORDER BY MAX(id) DESC
Related