我有一个像这样的mysql表:
id employee_number name
1 1 A
2 2 B
3 3 C
4 3 C
5 4 D
6 5 E
7 5 E
8 6 F
9 7 G
8 8 H
9 9 I
10 ....
11 ....
12 ....
many duplicated
employee_number
我想删除1行employee_number 3和5(id 3或4和6或7无关紧要)。 我可以使用以下sql查看重复的行,但不知道下一步该怎么做。如果有人可以帮我解决这个问题,我会说。非常感谢!
SELECT COUNT( id ) , employee_number
FROM `employees`
GROUP BY employee_number
更新
CREATE TABLE new_table as SELECT * FROM old_table WHERE 1 GROUP BY employee_number;
好。我找到了这个陈述并且它有效。多谢你们。
答案 0 :(得分:1)
DELETE FROM `employees`
WHERE ID
NOT IN
(SELECT MAX(B.ID) FROM
(SELECT employee_number
FROM `employees`
GROUP BY employee_number
HAVING COUNT(*)>1) A,
`employees` B
WHERE A.employee_number=B.employee_number
GROUP BY B.employee_number
);
答案 1 :(得分:1)
你可以像这样使用SQL(在oracle中): 查询重复的行 SELECT * FROM employees t1,其中t1.rowid> (SELECT min(t2.rowid)FROM employees t2,其中t2.name = t1.name);
删除fromv employees t1,其中t1.rowid>(从员工t2中选择min(t2.rowid),其中t2.no = t1.no);
答案 2 :(得分:0)
您可以自行加入表格。
假设不删除相同的记录。即,(不是Employee.id = vtable.id)
如果不是相同的记录,如果employee_number匹配,则删除。
delete from Employee
USING Employee, Employeeas vtable
WHERE (NOT Employee.id=vtable.id)
AND (Employee.employee_number=vtable.employee_number)