我有一个db表,如:
----------------------------
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
我希望获取最高(每只猫的最新插入记录),例如:
----------------------------
id name cat
============================
7 23.gif logo
9 19.gif web
为此我查询:
SELECT id, name, cat FROM portfolio GROUP BY cat ORDER BY name DESC
但是因为name字段是一个字符串所以我不能在名字上应用ORDER BY。
任何想法..?
答案 0 :(得分:1)
SELECT id, name, cat
FROM portfolio
GROUP BY cat
ORDER BY cast(substring(name, 1, INSTR(name, '.') - 1) as signed) DESC
答案 1 :(得分:0)
是的,我找到了解决方案:
select id, name, cat from (select * from portfolio ORDER BY id DESC) AS x GROUP BY cat
我从Retrieving the last record in each group获得了帮助
感谢Stackoverflow真正让开发人员生活更轻松:)