I have a db table like:
----------------------------
id name cat
============================
1 20.gif logo
2 21.gif logo
3 22.gif logo
4 15.gif web
5 16.gif web
6 17.gif web
7 23.gif logo
8 18.gif web
9 19.gif web
and I want to fetch highest (latest inserted record for each cat) some thing like:
----------------------------
id name cat
============================
7 23.gif logo
9 19.gif web
for this I queried:
SELECT id, name, cat FROM portfolio GROUP BY cat ORDER BY name DESC
but because name field is an string so I cant apply ORDER BY on name.
any idea..?
SELECT id, name, cat
FROM portfolio
GROUP BY cat
ORDER BY cast(substring(name, 1, INSTR(name, '.') - 1) as signed) DESC
Yes I found the solution :
select id, name, cat from (select * from portfolio ORDER BY id DESC) AS x GROUP BY cat
I got help from Retrieving the last record in each group
Thanks to Stackoverflow which really make developers life easy :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.