> 唯美句子 > SQL语句删除重复的记录

SQL语句删除重复的记录

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

一个表中有重复记录如何用SQL语句查询出来。。。?

select * from tablename where 重复字段1 in (select 重复字段1 from tablename group by 重复字段1,重复字段2 having count(*)>1)。

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 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

SQL语句查询 如何删除重复多余的数据

这个是SQL中distinct的典型用法:

1)从字面意思就可以了解到:

distinct [dis'tiŋkt] adj. 明显的;独特的;清楚的;有区别的

2)在SQL中用distinct来消除重复出现的字段值。

使得每个字段值只出现一次。

具体用法如下:

select distinct 字段名 from 表;

distinct 字段名 意思就是只显示一次该字段名

一般情况下和order by 结合使用,这样可以提高效率。

所以这个问题的答案是:select distinct 1,2,3,4 from 表;

1,2,3,4分别代表第一,二,三,四列的字段名,我猜测可能第一列就是每个人的ID,

这样你把重复的ID过滤留下一个,估计就是你想要的结果了。

希望我的回答能让您满意。

SQL查询语句,怎样查询重复数据

selectid,name,memo

fromA

whereidin(selectidfromAgroupbyidhavingcount(1)>=2)

1查询 abcd相同的记录:

select * from F where a=b and b=c and c=d

2查询有重复数据的记录

select * from F group by a,b,c,d having count(*)>1

3取出数据过滤到重复的数据

select distinct a,b,c,d from f

怎么利用SQL语句查询数据库中具体某个字段的重复行

我一般用这个:

假设怀疑重复的字段名为SeriNo,

select * from [tablename]

group by Se功iNo

having count(SeriNo)1

sql语句去重

sql 单表/多表查询去除重复记录

单表distinct

多表group by

group by 必须放在 order by 和 limit之前,不然会报错

************************************************************************************

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)

(二)

比方说

在A表中存在一个字段“name”,

而且不同记录之间的“name”值有可能会相同,

现在就是需要查询出在该表中的各记录之间,“name”值存在重复的项;

Select Name,Count(*) From A Group By Name Having Count(*) > 1

如果还查性别也相同大则如下:

Select Name,sex,Count(*) From A Group By Name,sex Having Count(*) > 1

(三)

方法一

declare @max integer,@id integer

declare cur_rows cursor local for select 主字段,count(*) from 表名 group by 主字段 having count(*) >; 1

open cur_rows

fetch cur_rows into @id,@max

while @@fetch_status=0

begin

select @max = @max -1

set rowcount @max

delete from 表名 where 主字段 = @id

fetch cur_rows into @id,@max

end

close cur_rows

set rowcount 0

方法二

"重复记录"有两个意义上的重复记录,一是完全重复的记录,也即所有字段均重复的记录,二是部分关键字段重复的记录,比如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)

最后一个select即得到了Name,Address不重复的结果集(但多了一个autoID字段,实际写时可以写在select子句中省去此列)

(四)

查询重复

select * from tablename where id in (select id from tablename

group by id

having count(id) > 1

)

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

select * from vitae a

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

运行会产生问题,where(a.peopleId,a.seq)这样的写发是通不过的!!!

mysql的sql语句如何去掉重复的数据

难倒只能分步操作,蛋疼

以下是网友写的,同样是坑爹的代码,我机器上运行不了。

1. 查询需要删除的记录,会保留一条记录。

?

1

select a.id,a.subject,a.RECEIVER from test1 a left join (select c.subject,c.RECEIVER ,max(c.id) as bid from test1 c where status=0 GROUP BY RECEIVER,SUBJECT having count(1) >1) b on a.id< b.bid where a.subject=b.subject and a.RECEIVER = b.RECEIVER and a.id < b.bid

2. 删除重复记录,只保留一条记录。注意,subject,RECEIVER 要索引,否则会很慢的。

?

1

delete a from test1 a, (select c.subject,c.RECEIVER ,max(c.id) as bid from test1 c where status=0 GROUP BY RECEIVER,SUBJECT having count(1) >1) b where a.subject=b.subject and a.RECEIVER = b.RECEIVER and a.id < b.bid;

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

?

1

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

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

?

如何用一条SQL语句查询数据库重复记录

方法如下:

select * from 你的表名

a where id=(select min(id) from 你的表名 whereitem_id=a.item_id)

在查询之前先把数据库表中的第一行复制到sid里在去,然后让sid和下面的每一行进行比较

取所有相同的行的最小的一下,也可以取最大的,结果是一样的。

这样让所有的行都比较不就得到不重复的数据了。

sql中怎么删除两条重复记录并保留一条

表结构如下

test (id int,name varchar2,age int)

1 张三 10

2 张三 10

3 李四 20

4 李四 20

5 王五 19

除了id外其他字段全部相同的记录只保留一条 删除多余的记录

delete test where id not in(select max(id) from test group by name,age)

如果没有id ,就用rowid

delete test where rowid not in(select max(rowid) from test group by id,name,age) --注意group by后的字段.

sql查询语句过滤重复数据。

SELECT Id,SiteId,InsertTime,IP,Referrer,Url

FROM

(

SELECT ROW_NUMBER()OVER(PARTITION BY IP ORDER BY Id DESC) number,

Id,SiteId,InsertTime,IP,Referrer,Url

From YourTable

)T

where number = 1

拿走不谢

SQL语句删除重复的记录:等您坐沙发呢!

发表评论

您必须 [ 登录 ] 才能发表留言!