SQL去除重复记录 if not object_id('Tempdb..#T') is null drop table #T Go Create table #T([ID] int,[Name] nvarchar(1),[Memo] nvarchar(2)) Insert #T select 1,N'A',N'A1' union all select 2,N'A',N'A2' union all select 3,N'A',N'A3' union all select 4,N'B',N'…
1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断select * from peoplewhere peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)2.删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录delete from people where peopleId in (sele…
例1,表中有主键(可唯一标识的字段),且该字段为数字类型 1 测试数据 /* 表结构 */ DROP TABLE IF EXISTS `t1`; CREATE TABLE IF NOT EXISTS `t1`( `id` ) NOT NULL AUTO_INCREMENT, `name` ) NOT NULL, `) NOT NULL, PRIMARY KEY(`id`) )Engine=InnoDB; /* 插入测试数据 */ INSERT INTO `t1`(`name`,`add`) VA…
第一步:查询重复记录 SELECT * FROM TableName WHERE RepeatFiled IN ( SELECT RepeatFiled FROM TableName GROUP BY RepeatFiled HAVING COUNT(RepeatFiled) > 1 ) 这一段逻辑很简单,就是把重复条数大于1的全部都搞出来就行了. 第二步:删除重复记录,只保留一条 SELECT * FROM TableName WHERE RepeatFil…
单表distinct 多表group by group by 必须放在 order by 和 limit之前,不然会报错 ************************************************************************************ 1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select * from people where peopleId in (select peopleId from peop…
单表distinct 多表group by group by 必须放在 order by 和 limit之前,不然会报错 下面先来看看例子: table id name 1 a 2 b 3 c 4 c 5 b 库结构大概这样,这只是一个简单的例子,实际情况会复杂得多. 比如我想用一条语句查询得到name不重复的所有数据,那就必须使用distinct去掉多余的重复记录. select distinct name from table得到的结果是: name a b …