0

如果一个表是这样的:

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中不能同时执行update和select。

delete from table where id not in (select max(id) from table group by complains, zipcode)
4

1 回答 1

0

您可以使用子选择来欺骗 MySQL:

delete from table 
where id not in 
(
     select * from (select max(id) from table group by complains, zipcode) x
)
于 2012-11-24T02:28:36.787 回答