1、查找表中多余的重復(fù)記錄,重復(fù)記錄是根據(jù)單個(gè)字段(peopleId)來判斷
創(chuàng)新互聯(lián)主要從事做網(wǎng)站、網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)翁源,十余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18982081108
select
* from people
where peopleId in (select peopleId from
people group by peopleId having count(peopleId)
1)
2、刪除表中多余的重復(fù)記錄,重復(fù)記錄是根據(jù)單個(gè)字段(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、查找表中多余的重復(fù)記錄(多個(gè)字段)
select * from vitae a
where (a.peopleId,a.seq)
in (select peopleId,seq from vitae group by peopleId,seq having
count(*) 1)
4、刪除表中多余的重復(fù)記錄(多個(gè)字段),只留有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、查找表中多余的重復(fù)記錄(多個(gè)字段),不包含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表中存在一個(gè)字段“name”,
而且不同記錄之間的“name”值有可能會(huì)相同,
現(xiàn)在就是需要查詢出在該表中的各記錄之間,“name”值存在重復(fù)的項(xiàng);
Select
Name,Count(*) From A Group By Name Having Count(*) 1
如果還查性別也相同大則如下:
Select Name,sex,Count(*) From A Group By Name,sex Having
Count(*) 1
b. 方法:
☆根據(jù)dname分組,查找出deptno最小的。然后再查找deptno不包含剛才查出來的。這樣就查詢出了所有的重復(fù)數(shù)據(jù)(除了deptno最小的那行)
方法2
刪除重復(fù)的行
單個(gè)字段的如果會(huì)了,多個(gè)字段也非常簡(jiǎn)單。就是將group by 的字段增加為你想要的即可。
此處只寫一個(gè),其他方法請(qǐng)仿照一個(gè)字段的寫即可。
查詢結(jié)果不含指定字段重復(fù)
2.表需要?jiǎng)h除重復(fù)的記錄(重復(fù)記錄保留1條),
3.查詢重復(fù)
4.1、查找表中多余的重復(fù)記錄,重復(fù)記錄是根據(jù)單個(gè)字段(peopleId)來判斷
4.2、刪除表中多余的重復(fù)記錄,重復(fù)記錄是根據(jù)單個(gè)字段(peopleId)來判斷,只留有rowid最小的記錄
4.3、查找表中多余的重復(fù)記錄(多個(gè)字段)
4.4、刪除表中多余的重復(fù)記錄(多個(gè)字段),只留有rowid最小的記錄
4.5、查找表中多余的重復(fù)記錄(多個(gè)字段),不包含rowid最小的記錄
4.6.消除一個(gè)字段的左邊的第一位:
4.7.消除一個(gè)字段的右邊的第一位:
4.8.假刪除表中多余的重復(fù)記錄(多個(gè)字段),不包含rowid最小的記錄
查詢重復(fù)
select id,count(1) 重復(fù)次數(shù) from A group by id having count(1)1;
查詢出來的結(jié)果都是id重復(fù)的,重復(fù)次數(shù) 中的數(shù)值就是重復(fù)了多少次。
1、最直觀的思路:要知道所有名字有重復(fù)人資料,首先必須知道哪個(gè)名字重復(fù)了:
select name from emp group by name having count(*)1
所有名字重復(fù)人的記錄是:
select * from emp
where name in (select name from emp group by name having count(*)1)
2、稍微再聰明一點(diǎn),就會(huì)想到,如果對(duì)每個(gè)名字都和原表進(jìn)行比較,大于2個(gè)人名字與這條記錄相同的就是合格的 ,就有:
select * from emp
where (select count(*) from emp e where e.name=emp.name) 1
select first_name,
case flag1+flag2 when 100 then '是' else null end as '兩邊都出現(xiàn)一次',
case flag1+flag2 when 101 then '是' else null end as 'phone出現(xiàn)多次',
case flag1+flag2 when 110 then '是' else null end as 'key出現(xiàn)多次',
case flag1+flag2 when 111 then '是' else null end as '兩邊都出現(xiàn)多次'
from (select first_name,
case when count(distinct number_key)=1 then 0 else 1 end as flag1,
case when count(distinct phone)=1 then 100 else 110 end as flag2
from table_name
group by first_name
) t
示例,創(chuàng)建數(shù)據(jù)表stuinfo,有三個(gè)字段recno(自增),stuid,stuname:
CREATE TABLE [StuInfo] ([recno] [int] IDENTITY (1, 1) NOT NULL ,[stuid] [varchar] (10) COLLATE Chinese_PRC_CI_AS NOT NULL ,[stuname] [varchar] (10) COLLATE Chinese_PRC_CI_AS NOT NULL) ON [PRIMARY]GO
一、查某一列(或多列)的重復(fù)值。(只可以查出重復(fù)記錄的值,不能查出整個(gè)記錄的信息)
例如:查找stuid,stuname重復(fù)的記錄:
select stuid,stuname from stuinfogroup by stuid,stunamehaving(count(*))1
二、查某一列有重復(fù)值的記錄。(此方法查出的是所有重復(fù)的記錄,如果有兩條記錄重復(fù)的,就查出兩條)
例如:查找stuid重復(fù)的記錄:
select * from stuinfowhere stuid in (select stuid from stuinfogroup by stuidhaving(count(*))1)
三、查某一列有重復(fù)值的記錄。(只顯示多余的記錄,也就是說如果有三條記錄重復(fù)的,就顯示兩條)
前提:需有一個(gè)不重復(fù)的列,此示例為recno。例如:查找stuid重復(fù)的記錄:
select * from stuinfo s1where recno not in (select max(recno) from stuinfo s2where s1.stuid=s2.stuid