sql server 刪除重復(fù)的數(shù)據(jù)
標(biāo)簽:
SQL Server
1--删除name,sex重复的数据,只留下一条数据;
select * into #lin1 from student --先将student的数据插入临时表#lin1中;
select min(id) as iidd into #lin2 from student group by name,sex --根据name,sex分组,并获取每组中最小的id;
select * from student where id not in(select iidd from #lin2) --确认查询出的这些数据,是否是重复的并且可以删除的数据;
delete student where id not in(select iidd from #lin2) --确认无误后,删除重复的数据;
select * from student --查看数据;
insert into student select * from #lin1 --删除后又想回复删除前的数据时,执行这条语句;
drop table #lin1
drop table #lin2 --最后删除这两张临时表;
2--只删除一个字段(name)的重复数据时;
select min(id) from student group by name
delete student where id not in (select min(id) from student group by name)
将删除语句整合成一条:
DELETE a FROM student AS a JOIN (SELECT name,COUNT(*) AS num,MAX(id) FROM student GROUP BY name HAVING COUNT(*)>1) AS b ON a.name=b.name WHERE a.id<b.id;
點(diǎn)擊查看更多內(nèi)容
10人點(diǎn)贊
評(píng)論
評(píng)論
共同學(xué)習(xí),寫下你的評(píng)論
評(píng)論加載中...
作者其他優(yōu)質(zhì)文章
正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦