在SQL表中查找重复值

本文翻译自:Finding duplicate values in a SQL table

It's easy to find duplicates with one field: 使用一个字段很容易找到duplicates

SELECT name, COUNT(email) 
FROM users
GROUP BY email
HAVING COUNT(email) > 1

So if we have a table 所以,如果我们有一张桌子

ID   NAME   EMAIL
1    John   asd@asd.com
2    Sam    asd@asd.com
3    Tom    asd@asd.com
4    Bob    bob@asd.com
5    Tom    asd@asd.com

This query will give us John, Sam, Tom, Tom because they all have the same email . 这个查询将给我们John,Sam,Tom,Tom,因为他们都有相同的email

However, what I want is to get duplicates with the same email and name . 但是,我想要的是使用相同的emailname获取重复项。

That is, I want to get "Tom", "Tom". 也就是说,我想得到“汤姆”,“汤姆”。

The reason I need this: I made a mistake, and allowed to insert duplicate name and email values. 我需要这个的原因:我犯了一个错误,并允许插入重复的nameemail值。 Now I need to remove/change the duplicates, so I need to find them first. 现在我需要删除/更改重复项,所以我需要先找到它们。


#1楼

参考:https://stackoom.com/question/At25/在SQL表中查找重复值


#2楼

Try the following: 请尝试以下方法:

SELECT * FROM
(
    SELECT Id, Name, Age, Comments, Row_Number() OVER(PARTITION BY Name, Age ORDER By Name)
        AS Rank 
        FROM Customers
) AS B WHERE Rank>1

#3楼

In case you work with Oracle, this way would be preferable: 如果您使用Oracle,这种方式更可取:

create table my_users(id number, name varchar2(100), email varchar2(100));

insert into my_users values (1, 'John', 'asd@asd.com');
insert into my_users values (2, 'Sam', 'asd@asd.com');
insert into my_users values (3, 'Tom', 'asd@asd.com');
insert into my_users values (4, 'Bob', 'bob@asd.com');
insert into my_users values (5, 'Tom', 'asd@asd.com');

commit;

select *
  from my_users
 where rowid not in (select min(rowid) from my_users group by name, email);

#4楼

If you wish to see if there is any duplicate rows in your table, I used below Query: 如果你想看看你的表中是否有任何重复的行,我使用下面的Query:

create table my_table(id int, name varchar(100), email varchar(100));

insert into my_table values (1, 'shekh', 'shekh@rms.com');
insert into my_table values (1, 'shekh', 'shekh@rms.com');
insert into my_table values (2, 'Aman', 'aman@rms.com');
insert into my_table values (3, 'Tom', 'tom@rms.com');
insert into my_table values (4, 'Raj', 'raj@rms.com');


Select COUNT(1) As Total_Rows from my_table 
Select Count(1) As Distinct_Rows from ( Select Distinct * from my_table) abc 

#5楼

try this code 试试这段代码

WITH CTE AS

( SELECT Id, Name, Age, Comments, RN = ROW_NUMBER()OVER(PARTITION BY Name,Age ORDER BY ccn)
FROM ccnmaster )
select * from CTE 

#6楼

SELECT
    name, email, COUNT(*)
FROM
    users
GROUP BY
    name, email
HAVING 
    COUNT(*) > 1

Simply group on both of the columns. 只需在两个列上分组。

Note: the older ANSI standard is to have all non-aggregated columns in the GROUP BY but this has changed with the idea of "functional dependency" : 注意:旧的ANSI标准是在GROUP BY中包含所有非聚合列,但这已经改变了“功能依赖”的概念

In relational database theory, a functional dependency is a constraint between two sets of attributes in a relation from a database. 在关系数据库理论中,函数依赖性是来自数据库的关系中的两组属性之间的约束。 In other words, functional dependency is a constraint that describes the relationship between attributes in a relation. 换句话说,函数依赖是描述关系中属性之间关系的约束。

Support is not consistent: 支持不一致:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值