精华内容
下载资源
问答
  • 要求,编写一个SQL查询查找Person表中所有重复的电子邮箱 Create table If Not Exists Person (Id int, Email varchar(255)) Truncate table Person insert into Person (Id, Email) values ('1', 'a@b.com') insert...

    查找重复的电子邮箱

    题目来源:力扣182. 查找重复的电子邮箱
    要求,编写一个SQL查询查找Person表中所有重复的电子邮箱

    Create table If Not Exists Person (Id int, Email varchar(255))
    Truncate table Person
    insert into Person (Id, Email) values ('1', 'a@b.com')
    insert into Person (Id, Email) values ('2', 'c@d.com')
    insert into Person (Id, Email) values ('3', 'a@b.com')
    

    在这里插入图片描述
    方法一:
    使用group by 和辅助表

    select Email from
        (select Email,count(Email) as num
        from Person
        group by Email) as statistic
        where num>1;
    

    方法二:group by 和 having条件

    select Email 
    	from Person
    	group by Email
    	having(count(Email)>1);
    

    这里需要注意
    使用以下语句是错误的,因为 where 子句不能与聚合函数一起使用
    它们的执行顺序:where>group by>having>order by,如果where语句与count()一起使用的时候group by 还没有执行,无法使用counting

    select Email 
        from Person
    	group by Email
        where count(Email)>1;
    

    删除重复电子邮箱

    题目来源:力扣196. 删除重复的电子邮箱
    编写一个 SQL 查询,来删除 Person 表中所有重复的电子邮箱,重复的邮箱里只保留 Id 最小 的那个。
    在这里插入图片描述
    方法一:使用delete+子查询
    第一步:对Email进行分组,并将最小的id拿出来
    select min(Id) from person group by Email;
    第二步:把它保存为一个虚拟表
    (select min(Id) from Person group by Email) as need;
    第三步:把need表中的id提取出来

    select need.id from 
    	(select min(Id) as Id
    	from Person
    	group by Email)
    	as need;
    

    第四步:删除不在need中的id中的数据

    delete from Person
    delete from Person
    	where Id 
        not in
    	(select need.Id from 
            (
            select min(Id) as Id
            from Person
            group by Email
            )as need
        );
    

    方法二:使用表的自连接

    DELETE p1 FROM Person p1,
        Person p2
    WHERE
        p1.Email = p2.Email AND p1.Id > p2.Id
    

    这里需要注意的一个是deletezhong使用表的别名:
    参考Mysql DELETE 不能使用别名? 是我不会用!
    该题目也不可以使用distinct来解决,关于distinct的文章可参考:
    SQL中distinct的用法
    参考链接
    一只猪的解题思路
    删除重复的电子邮箱

    展开全文
  • 在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢 1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select * from people where peopleId in (select peopleId from ...
        在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢  
        1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断  
        select * from people  
        where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)  
          
        2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录  
        delete from people  
        where   peopleName in (select peopleName    from people group by peopleName      having count(peopleName) > 1)  
        and   peopleId not in (select min(peopleId) from people group by peopleName     having count(peopleName)>1)  
          
        3、查找表中多余的重复记录(多个字段)  
        select * from vitae a  
        where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)  
          
        4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录  
        delete from vitae a  
        where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)  
        and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)  
          
        5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录  
        select * from vitae a  
        where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)  
        and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)     
          
        6.消除一个字段的左边的第一位:  
          
        update tableName set [Title]=Right([Title],(len([Title])-1)) where Title like '村%'  
          
        7.消除一个字段的右边的第一位:  
          
        update tableName set [Title]=left([Title],(len([Title])-1)) where Title like '%村'  
          
        8.假删除表中多余的重复记录(多个字段),不包含rowid最小的记录  
        update vitae set ispass=-1  
        where peopleId in (select peopleId from vitae group by peopleId  
    

    看完上面相信大家懂了大概 ,接下来实例代码分析

    在这里插入图片描述在这里插入图片描述


    1先查找出重复的( 和2的删除功能没有必然联系 只是看看效果)

    SELECT
      *
    FROM
        SensitiveWord
    	WHERE
       KeyWord IN
        (
            SELECT
                KeyWord
            FROM
                SensitiveWord
            GROUP BY
                KeyWord
            HAVING
                COUNT(*) > 1
        )
    
    

    在这里插入图片描述-------------------

    2.删除重复数据

    delete from SensitiveWord where KeyWord in (select KeyWord from SensitiveWord group by KeyWord having count(KeyWord) > 1) and Id not in (select min(Id) from SensitiveWord group by KeyWord having count(KeyWord)>1)
    在这里插入图片描述结果完美
    在这里插入图片描述

    展开全文
  • sql 查询重复数据

    2013-01-23 13:45:28
    sql 查询重复数据 可以筛选相关重复信息 网上找到
  • sql语句查询重复数据

    千次阅读 2021-05-19 09:50:15
    查找所有重复标题的记录: SELECT * FROM t_info a WHERE ((SELECT COUNT(*) FROM t_info WHERE Title = a.Title) > 1) ORDER BY Title DESC 一。查找重复记录 1。查找全部重复记录 Select * From 表 Where 重复...

    查找所有重复标题的记录:

    SELECT *
    FROM t_info a
    WHERE ((SELECT COUNT(*)
    FROM t_info
    WHERE Title = a.Title) > 1)
    ORDER BY Title DESC

    一。查找重复记录

    1。查找全部重复记录

    Select * From 表 Where 重复字段 In (Select 重复字段 From 表 Group By 重复字段 Having Count(*)>1)

    2。过滤重复记录(只显示一条)

    Select * From HZT Where ID In (Select Max(ID) From HZT Group By Title)

    注:此处显示ID最大一条记录

    二。删除重复记录

    1。删除全部重复记录(慎用)

    Delete 表 Where 重复字段 In (Select 重复字段 From 表 Group By 重复字段 Having Count(*)>1)

    2。保留一条(这个应该是大多数人所需要的 _

    Delete HZT Where ID Not In (Select Max(ID) From HZT Group By Title)

    注:此处保留ID最大一条记录

    1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断

    select * from people

    where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)

    2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录

    delete from people

    where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)

    and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )>1)

    3、查找表中多余的重复记录(多个字段)

    select * from vitae a

    where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)

    4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录

    delete from vitae a

    where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)

    and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)

    5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录

    select * from vitae a

    where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)

    and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)

    补充:

    有两个以上的重复记录,一是完全重复的记录,也即所有字段均重复的记录,二是部分关键字段重复的记录,比如Name字段重复,而其他字段不一定重复或都重复可以忽略。

    1、对于第一种重复,比较容易解决,使用

    select distinct * from tableName

    就可以得到无重复记录的结果集。

    如果该表需要删除重复的记录(重复记录保留1条),可以按以下方法删除

    select distinct * into #Tmp from tableName

    drop table tableName

    select * into tableName from #Tmp

    drop table #Tmp

    发生这种重复的原因是表设计不周产生的,增加唯一索引列即可解决。

    2、这类重复问题通常要求保留重复记录中的第一条记录,操作方法如下

    假设有重复的字段为Name,Address,要求得到这两个字段唯一的结果集

    select identity(int,1,1) as autoID, * into #Tmp from tableName

    select min(autoID) as autoID into #Tmp2 from #Tmp group by Name,autoID

    select * from #Tmp where autoID in(select autoID from #tmp2)

    展开全文
  • 1.按多个字段查询: select "POSITION_NAME","CMEMO","SUPERINTENDENT_MAN_NAME","SUPERINTENDENT_MAN_NAME" from "DCS_RISK" where ("POSITION_NAME","CMEMO") in (select "POSITION_NAME","CMEMO" from "DCS_RISK...

    1.按多个字段查询:

    select "POSITION_NAME","CMEMO","SUPERINTENDENT_MAN_NAME","SUPERINTENDENT_MAN_NAME" from  "DCS_RISK"
    where ("POSITION_NAME","CMEMO") in (select  "POSITION_NAME","CMEMO" from   "DCS_RISK"   group by "POSITION_NAME","CMEMO"    having count (*) > 1) ORDER BY "POSITION_NAME" DESC,"CMEMO" DESC

     

    1.按单个字段查询:

    select "POSITION_NAME","CMEMO","SUPERINTENDENT_MAN_NAME","SUPERINTENDENT_MAN_NAME" from  "DCS_RISK"
    where "ID" in (select  "ID" from   "DCS_RISK"  group by "ID"    having count ("ID") > 1)

     

    展开全文
  • SQL查询重复数据

    2020-11-12 15:06:38
    1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select * from 表名称where peopleId in (select 字段名 from 表名称 group by 字段名having count(字段名) > 1) 2、查找表中多余的...
  • SQL找出重复数据只保留一条

    千次阅读 2017-12-30 00:47:00
    SQL找出重复数据只保留一条 博客分类: sqlSQL语句,删除掉重复项只保留一条 在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢 1、查找表中多余的重复记录,重复记录是根据...
  • sql 查出一张表中重复...2、查询所有数据进行分组之后,和重复数据的重复次数的查询数据,先列下: select count(username) as '重复次数',username from xi group by username having count(*)>1 order by userna
  • 如何利用sql查找表中的重复数据

    千次阅读 2019-11-04 14:29:20
    编写一个SQL查询,查找学生表中所有重复的学生名; 分析思路 1.看到“找重复”的关键字眼,首先要用分组函数(group by),再用聚合函数中的计数函数count()给姓名列计数。 2. 分组汇总后,生成了一个如下的表。...
  • SQL查询重复记录

    万次阅读 多人点赞 2018-06-04 09:28:23
    1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select * from people where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1) 2、删除...
  • SQL实现找出重复数据 GROUP BY+ HAVING SELECT 字段 FROM 表 GROUP BY 需要的重复字段 HAVING COUNT(1)>1
  • SELECT F_Name,count(F_Name) FROM `t_business_person` where F_Lib_ID=000001 GROUP BY F_Name HAVING COUNT(F_Name)>1 ORDER BY COUNT(F_Name);
  • SqlServer查询重复数据

    千次阅读 2020-09-24 11:23:03
    1、查询单列重复 select * from lcgl_task_ti_b where prn in (select prn from lcgl_task_ti_b group by prn having count (prn) > 1) 2、查询多列重复的 select a.* from lcgl_task_ti_b a,(select order_prn,...
  • MySQL查询表中重复数据,比如现在需要查询表中条码重复的记录,SQL如下:SELECT GUIDE_BARCODE FROM checker_barcodeGROUP BY GUIDE_BARCODE HAVING COUNT(GUIDE_BARCODE) >1现在查询重复的数据的sql是出来了,...
  • ![图片说明](https://img-ask.csdn.net/upload/201803/19/1521431726_363194.jpg)
  • C# 用SQL自动记录数据并且查询某一列有哪些重复数据,本人实际测试能用,欢迎下载使用!
  • sqlserver 查询重复数据

    2021-08-05 10:41:38
    --单字段 SELECT * from (select (t.memberName + t.argot) as aaa FROM agom t where t.status = 0) b group by b.aaa having count(b.aaa) > 1 ...SELECT b.* FROM agom a,( SELECT memberName,argot ...
  • SQL Select 删除重复数据 保留最大id

    千次阅读 2019-02-27 11:10:38
    1,查询Table1 中重复数据,(不包含重复数据中id最大的一行)(效率比较高) --查询重复数据 SELECT * FROM Table t1 WHERE EXISTS (SELECT id FROM Table t2 WHERE t1.列1=t2.列1 AND t1.列2=t2.列2 AND t1.列3=...
  • SQL 查找重复记录

    千次阅读 2019-02-17 15:34:33
    SQL 查找重复记录
  • sql查询重复数据

    万次阅读 2018-10-12 10:03:53
    SELECT 重复的字段 FROM 查询的表名 GROUP BY 重复的字段 HAVING COUNT(重复的字段)>1 最后的大于1表示查询大于重复一次得数据
  • 只能删除重复次数为2的记录,多条重复记录时候,需要多次执行 delete from 表名 where id in (select a.id from (select 字段1,字段2,min(id) id from 表名 group ...1 为查询出重复数据。字段1,2都相同才会认为是重复
  • 在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select * from people where peopleId in (select peopleId from people ...
  • sql 查询id重复

    2020-12-16 16:36:59
    Select * From 表 Where ID in(select ID from ID group by ID having count(ID)>1) count 求总数
  • SQL Server获取重复数据的方法

    万次阅读 2018-08-17 08:42:13
    一 获取重复的数据的值 select user_name from users group by user_name having(count(1)>1) 二 获取重复数据的记录 select * from users where user_name ...
  • select distinct username,quantity from Student where isnull(username,'')<>'' and p_id = {1} 转载于:https://www.cnblogs.com/superMay/p/6197975.html
  • sql筛选重复数据

    万次阅读 2017-07-12 16:41:36
    1、单个字段去除重复值select distinct '字段名' from '表名'2、如果需求不仅仅是找出某一个字段的唯一值,我们还想查看一下,这个字段拥有重复值的几条数据,进一步分析重复的原因,那就需要用到下面的语句:select...
  • --删除重复的客户数据 update [dbo].[Customer] set Deleted=1, [Note]=ISNULL([Note]... --查找重复的 a.PartnerId,a.SourceId 数据 select a.Id from [dbo].[Customer] a left join ( --查找重复的 a.Part..
  • 假设现有一张人员表(表名:Person),若想将姓名、身份证号、住址这三个字段完全相同的记录查找出来,使用 1: SELECT p1.* 2: FROM persons p1,persons p2 3: WHERE p1.id 4: AND p1.cardid = p2....

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 204,654
精华内容 81,861
关键字:

sql查找重复数据