sql查找相同数据与删除相同记录中的一行

1. 查找数据表中某些字段相同的数据,查找相同数据:小技巧1:select * from tablename tn where((select count(*) from tablename where column1= tn.column1 and column2=tn.column2 ) > 1)提示:可以接order by 语句更方便查看

2.删除相同记录中的一行:小技巧1:当表中存在唯一键值的情况:delete from tablename where id not in(select max(id) from tablename group by column1,column2)小技巧2:不存在唯一键值的情况,最简单的方法当然是新增一个列id,然后用1中的方法了。不过可以用中间表存取的方式:首选建一个和a表一样结构的中间表,然后insert into b select distinct * from a,再删除表a,delete from a,最后把b中的数据插入到a中, insert into a select * from b

例如:表caicai ,字段id,title,content

 
select * from caicai where title in ( select title from caicai group by title having count(*) >1)
 
select * from caicai tn where((select count(*) from caicai where title= tn.title) > 1)
 
delete from caicai where id not in(select max(id) from caicai group by title)
 
SELECT infoid, title, brwsPath, indexed, indexed2 FROM infos WHERE (title IN(SELECT titleFROM infosGROUP BY titleHAVING COUNT(title) > 1)) ORDER BY title DESC
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值