如果表格是这样的:
id | complains | zipcode
--------------------------
1 | drinking | 10000
2 | drinking | 10000
3 | wasting | 10000
4 | wasting | 10000
5 | wasting | 10000
6 | wasting | 10011
7 | wasting | 10011
我想得到:
---------------
drinking 10000
wasting 10000
wasting 10011
---------------
我应该如何编写SQL查询?我写的是这样的,但更新和选择不能在mySQL中同时执行。
delete from table where id not in (select max(id) from table group by complains, zipcode)
答案 0 :(得分:0)
您可以使用子选择来欺骗MySQL:
delete from table
where id not in
(
select * from (select max(id) from table group by complains, zipcode) x
)