1、查詢表中多餘的重覆記錄,重覆記錄是根據單個字段(docid)來判斷
select*from tablename
where docid in (select docid from tablename group
by docid h**ing
count(docid) >
1)
例二:
select*from tablename
where username in (select username from tablename group
by tablename h**ing
count(tablename) >
1 )
可以查出表中username相同的記錄
2、刪除表中多餘的重覆記錄,重覆記錄是根據單個字段(columnname)來判斷,只留有docid最小的記錄
deletefrom tablename
where columnname in (select columnname from tablename group
by columnname h**ing
count(columnname) >
1)and docid not
in (select
min(docid) from tablename group
by columnname h**ing
count(columnname)>
1)
3、查詢表中多餘的重覆記錄(多個字段)
select*from tablename t
where (t.docid+t.columnname) in (select docid+columnname from tablename group
by docid,columnname h**ing
count(*) >
1)--
前提是「docid,columnname」為字元型,否則需要轉換 即:convert(varchar,docid)
4、刪除表中多餘的重覆記錄(多個字段),只留有rowid最小的記錄
deletefrom tablename t
where (t.columna+t.columnb) in (select columna+columnb from tablename group
by columna,columnb h**ing
count(*) >
1)and docid not
in (select
min(docid) from tablename group
by columna,columnb h**ing
count(*)>
1)--
前提是「docid,columnname」為字元型,否則需要轉換 即:convert(varchar,docid)
5、查詢表中多餘的重覆記錄(多個字段),不包含rowid最小的記錄
select*from tablename t
where (t.columna+t.columnb) in (select columna+columnb from tablename group
by columna,columnb h**ing
count(*) >
1)and docid not
in (select
min(docid) from tablename group
by columna,columnb h**ing
count(*)>
1)--
前提是「docid,columnname」為字元型,否則需要轉換 即:convert(varchar,docid)
SQL 刪除重覆記錄
例如 id name value 1 a pp 2 a pp 3 b iii 4 b pp 5 b pp 6 c pp 7 c pp 8 c iii id是主鍵 要求得到這樣的結果 id name value 1 a pp 3 b iii 4 b pp 6 c pp 8 c iii 方法1delet...
SQL刪除重覆記錄
星期五去一家公司面試sql資料庫工程師,有乙份筆試題目,其中有一題是 現在有乙個表t 姓名,手機號,手機號字段存在重覆記錄,要求用一句sql刪除重覆記錄。我想了想寫了一句建立唯一索引的語句 create unique index t index on t 手機號 交卷了,後來想想也不對,這樣只能保證...
sql 重覆記錄和重覆記錄數
如果table1有兩個column adress和pepole,那麼下面的sql可以找出table1裡的重覆記錄和重覆記錄數 create table table1 adress nvarchar 10 pepole nvarchar 10 insert table1 select 寧波 張三 nb...