我需要SQL查询来删除表中的重复记录

时间:2017-07-31 06:59:33

标签: mysql sql

----我的桌子-------

id       name         company      salary    
---- ------------- --------------  ----------
1        abc            xyz            10000

2        abc             x             10000

3        ab              y             15000

1        abc            xyz            10000

4        bc             yz             1000

3        ab              y             15000

5        ab              xy             15000

-----删除后我需要我的表格如下----

id       name         company      salary    
---- ------------- --------------  ----------
1        abc            xyz            10000

2        abc             x             10000

3        ab              y             15000

4        bc             yz             1000

5        ab             xy             15000

2 个答案:

答案 0 :(得分:0)

ALTER IGNORE TABLE `table_name` ADD UNIQUE (name,company,salary)**

或者您可以尝试以下查询。

DELETE CO
FROM table_name CO
INNER JOIN (
SELECT MIN(ID) ID FROM table_name GROUP BY name,company,salary HAVING count(ID) >1
)INNERTABLE
  ON INNERTABLE.ID=CO.ID;

答案 1 :(得分:0)

引入临时列以获得主键

ALTER TABLE t ADD tmp INT NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;

呼叫删除删除重复

delete t
from t
inner join t t2 on t.id=t2.id and t.tmp>t2.tmp

然后删除tmp列