我的表包含以下条目
Id | Accno | Name | Hash
----+----------+-----------+---------
1 | 11 | ABC | 01110
2 | 11 | ABC |
3 | 22 | PQT |
4 | 33 | XYZ | 03330
5 | 44 | LMN | 04440
6 | 33 | XYZ |
我需要SQL查询来从表中删除重复条目,并在表中存在哈希值的至少单个条目。对于那些不重复的条目,也应保留在表格中。
答案 0 :(得分:1)
我认为你们经常使事情过于复杂化。这应该只是花花公子:
DELETE FROM
YourTable
WHERE Hash IS NULL
AND Accno IN
(
SELECT Accno
FROM YourTable
GROUP BY Accno
HAVING COUNT(Name) > 1
)
;
答案 1 :(得分:0)
最简单的方法可能是创建一个新表并复制非重复的条目。
create table table_name2 as select distinct * from table_name1;
drop table table_name1;
rename table_name2 to table_name1;
像这样。
答案 2 :(得分:0)
Create table temp2 as SELECT *
FROM temp where id in (select id from temp group by accno having count(accno)>=1 and hash<>'');
drop table old_table;
rename table temp2 to old_table;