所以我有一个数据库,行数超过2000行,我想删除基于2行的重复值。
例如:
no server name
1 serv1 a
2 serv1 b
3 serv1 b
4 serv1 b
5 serv2 a
6 serv2 b
7 serv2 c
8 serv2 c
所以基本上我想删除重复的 IF 其中两行重复的
但我不想删除它们,如果只是其中一行有重复。
预期输出:
no server name
1 serv1 a
2 serv1 b
3 serv2 a
4 serv2 b
5 serv2 c
任何答案将不胜感激。
答案 0 :(得分:1)
使用select distinct
:
select distinct server, name
from t;
如果重复很多,最简单的方法可能是重新创建表:
create table temp_t as
select min(no) as no, server, name
from t
group by server, name;
truncate table t;
insert into t (no, server, name)
select no, server, name
from temp_t;
如果您没有很多行,那么delete
可以:
delete t
from t join
(select server, name, min(no) as min_no
from t
group by server, name
) tt
on t.server = tt.server and t.name = tt.name
where t.no > tt.min_no;
答案 1 :(得分:1)
有很多方法可以做您想要的。
如果您只是在寻找SELECT
没有重复的数据,则可以使用:
DISTINCT
SELECT DISTINCT Server,
Name
FROM YourTableName
组别
SELECT Server,
Name
FROM YourTableName
GROUP BY Server, Name
子查询中的窗口函数(ROW_NUMBER)
SELECT Server,
Name
FROM
(
SELECT *,
ROW_NUMBER() OVER(PARTITION BY Server, Name ORDER BY Server) RN
FROM YourTableName
) TT
WHERE TT.RN = 1
您将重复项删除为
DELETE T1
FROM
(
SELECT ROW_NUMBER() OVER(PARTITION BY Server, Name ORDER BY Server) RN
FROM T --YourTableName
) T1 JOIN
(
SELECT ROW_NUMBER() OVER(PARTITION BY Server, Name ORDER BY Server) RN
FROM T --YourTableName
) T2 ON T1.RN = T2.RN
WHERE T1.RN > 1;
答案 2 :(得分:0)
您可以使用以下查询删除重复项
DELETE t1 FROM tablename t1 INNER JOIN tablename t2
WHERE t1.id < t2.id AND t1.server = t2.server AND t1.name = t2.name;