1

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..?

PHP Ferrari
  • 15,754
  • 27
  • 83
  • 149

2 Answers2

1
SELECT id, name, cat 
FROM portfolio 
GROUP BY cat 
ORDER BY cast(substring(name, 1, INSTR(name, '.') - 1) as signed) DESC    

SQLFiddle example

juergen d
  • 201,996
  • 37
  • 293
  • 362
0

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 :)

Community
  • 1
  • 1
PHP Ferrari
  • 15,754
  • 27
  • 83
  • 149