Reputation: 15616
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..?
Upvotes: 1
Views: 189
Reputation: 15616
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 :)
Upvotes: 0
Reputation: 204746
SELECT id, name, cat
FROM portfolio
GROUP BY cat
ORDER BY cast(substring(name, 1, INSTR(name, '.') - 1) as signed) DESC
Upvotes: 1