1. 程式人生 > >sql 刪除表中多餘的重複記錄(多個欄位),只保留一條記錄

sql 刪除表中多餘的重複記錄(多個欄位),只保留一條記錄

在網上呢~自己收集了一些關於這方面的知識~  自己整理一下

1.查詢重複記錄

select * from 表名
where 重複欄位 in (select  重複欄位 from  表名  group  by  重複欄位 having  count(重複欄位) > 1)

2.刪除保留一條重複記錄只留有id最小的記錄 
delete from 表名
where 重複欄位  in (select  重複欄位 from 表名 group  by  重複欄位   having  count(重複欄位) > 1)
and ID not in (select min(ID) from  表名  group by 重複欄位 having count(重複欄位 )>1)
3,查詢表中多餘的重複記錄(多個欄位),不包含id(偽列)最小的記錄

select * from 表名
where 重複欄位 in (select 重複欄位 from 表名 group by 重複欄位 having count(*) > 1) 
and id not in (select min(id) from 表名 group by 重複欄位 having count(*)>1)   

4.消除一個欄位的左邊的第一位:
update 表名 set 消除欄位 =left(消除欄位,(len(消除欄位-1)) where 消除欄位 like '李%' 

5.消除一個欄位的右邊的第一位:

update 表名 set 消除欄位 =Right(消除欄位,(len(消除欄位-1)) where 消除欄位 like '%李'