简体   繁体   English

如何删除表中多字段的重复项?(mysql)

[英]how to delete duplicates for multi-fields in a table?(mysql)

If a table is like this: 如果一个表是这样的:

id | complains | zipcode
--------------------------
1  |  drinking |  10000
2  |  drinking |  10000
3  |  wasting  |  10000
4  |  wasting  |  10000
5  |  wasting  |  10000
6  |  wasting  |  10011
7  |  wasting  |  10011

I want to get just: 我只想得到:

---------------
drinking  10000
wasting   10000
wasting   10011
---------------

How should I write a SQL query? 我应该如何编写SQL查询? I wrote like this, but update and select cannot be execute at same time in mySQL. 我这样写,但是更新和选择不能在mySQL中同时执行。

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

You can use a subselect to trick MySQL: 您可以使用子选择来欺骗MySQL:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM