我正在尝试执行MySQL查询,我有2列id和代码,id是自动生成的。我绑的是下面的。我得到了我想要实现的目标。我尝试了太多查询组合Unique,Distinct等。
Query: "SELECT DISTINCT(code) FROM table GROUP BY code ORDER BY id DESC;"
id code
1 0000
2 0000
3 0001
4 0001
5 0001
6 0002
7 0003
What i am trying to do get
id code
2 0000
5 0001
6 0002
7 0003
答案 0 :(得分:0)
SELECT * FROM `table` WHERE id IN (SELECT MAX(id) FROM `table` GROUP BY `code` ORDER BY `id`)
对于DESC订单,请使用:
SELECT * FROM `table` WHERE id IN (SELECT MAX(id) FROM `table` GROUP BY `code`) ORDER BY `id` DESC