由于编程错误,我有重复的条目。
表示例:
id | group | userNum | username | name
---------------------------------------
1 | AA11 | D-01 | user1 | Donald
2 | AA11 | D-02 | user2 | Cruz
3 | AA11 | D-03 | user3 | Rubio
4 | AA11 | D-01 | user1 | Donald <------DUPLICATE
5 | AA11 | D-04 | user4 | Cruz
6 | AA22 | D-03 | user2 | Rubio
7 | AA22 | D-02 | user1 | Donald
userNum,用户名必须对每个组都是唯一的,但一个组可以拥有在其他组中找到的userNum,username,name。
答案 0 :(得分:1)
标准SQL方法是在WHERE
子句中使用子查询。例如:
delete from example
where id not in (select min(id)
from example e2
group by group, userNum, username, name
);
这在MySQL中不起作用。您可以使用left join
执行类似操作:
delete e
from example e left join
(select min(id) as minid
from example e2
group by group, userNum, username, name
) ee
on e.id = ee.minid
where ee.minid is null;
答案 1 :(得分:0)
在查询结尾处添加group by userNum, username, group
..