如何使用一條SQL語句刪除表中重復記錄
數據庫結構的腳本:if exists (select * from dbo。
sysobjects where id = object_id(N'[dbo]。[TempA]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo]。
[TempA]GO CREATE TABLE [dbo]。[TempA] ( [id] [int] IDENTITY (1, 1) NOT NULL , [PositionName] [varchar] (256) COLLATE Chinese_PRC_CI_AS NULL , [EnglishPositionName] [varchar] (256) COLLATE Chinese_PRC_CI_AS NULL ) ON [PRIMARY]GOALTER TABLE [dbo]。
[TempA] ADD CONSTRAINT [PK_TempA] PRIMARY KEY CLUSTERED ( [id] ) ON [PRIMARY] GOTempA表中有三個字段,id唯一且為主鍵,自動增長; PositionName,EnglishPositionName中有重復的記錄,比如:id PositionName EnglishPositionName20 其他 Others21 質量工程師 QC Engineer 22 其他 Others。 。
。100 質量工程師 QC Engineer 需要剔除重復的"其他","質量工程師"等記錄。
采用的SQL語句:Delete from TempA where id not in ( select max(t1。id) from TempA t1 group by t1。
PositionName,t1。EnglishPositionName)說明: (1)需要剔除那幾個用于判斷重復的字段,則將它們放在group by語句之后。
(2)max(t1。id) 也可以改成:min(t1。
id)。
如何用SQL語句去掉重復記錄
COL1 中有重復記錄(COL1,COL2為主鍵),如何刪除
1、有少數重復記錄(在col1,col2上有索引比較好)
DELETE T
WHERE (COL1,COL2) IN
(SELECT COL1,COL2 FROM T GROUP BY COL1,COL2 HAVING COUNT(*) >1)
AND
ROWID NOT IN
(SELECT MIN(ROWID) FROM T GROUP BY COL1,COL2 HAVING COUNT(*) >1)
2、大部份記錄有重復記錄
DELETE T WHERE ROWID NOT IN
(SELECT MIN(ROWID) FROM T GROUP BY COL1,COL2)
3、其他寫法
DELETE T WHERE ROWID IN
(SELECT * FROM T A,T B
WHERE *1=*1 AND *2 = *2 AND * >*)
######################################
10. 刪除重復記錄
最高效的刪除重復記錄方法 ( 因為使用了ROWID)
DELETE FROM EMP E
WHERE * >(SELECT MIN(*)
FROM EMP X
WHERE *_NO = *_NO);
11. 用TRUNCATE替代DELETE
當刪除表中的記錄時,在通常情況下, 回滾段(rollback segments ) 用來存放可以被恢復的信息. 如果你沒有COMMIT事務,ORACLE會將數據恢復到刪除之前的狀態(準確地說是
恢復到執行刪除命令之前的狀況)
而當運用TRUNCATE時, 回滾段不再存放任何可被恢復的信息.當命令運行后,數據不能被恢復.因此很少的資源被調用,執行時間也會很短.
(譯者按: TRUNCATE只在刪除全表適用,TRUNCATE是DDL不是DML)
12. 盡量多使用COMMIT
只要有可能,在程序中盡量多使用COMMIT, 這樣程序的性能得到提高,需求也會因為COMMIT所釋放的資源而減少:
COMMIT所釋放的資源:
a. 回滾段上用于恢復數據的信息.
b. 被程序語句獲得的鎖
c. redo log buffer 中的空間
d. ORACLE為管理上述3種資源中的內部花費
幾個刪除重復記錄的SQL語句
用SQL語句,刪除掉重復項只保留一條在幾千條記錄里,存在著些相同的記錄,如何能用SQL語句,刪除掉重復的呢1、查找表中多余的重復記錄,重復記錄是根據單個字段(peopleId)來判斷 select * from people where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1) 2、刪除表中多余的重復記錄,重復記錄是根據單個字段(peopleId)來判斷,只留有rowid最小的記錄 delete from people where peopleName in (select peopleName from people group by peopleName having count(peopleName) > 1) and peopleId not in (select min(peopleId) from people group by peopleName having count(peopleName)>1) 3、查找表中多余的重復記錄(多個字段) select * from vitae a where (*Id,*) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1) 4、刪除表中多余的重復記錄(多個字段),只留有rowid最小的記錄 delete from vitae a where (*Id,*) 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、查找表中多余的重復記錄(多個字段),不包含rowid最小的記錄 select * from vitae a where (*Id,*) 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) 6.消除一個字段的左邊的第一位: update tableName set [Title]=Right([Title],(len([Title])-1)) where Title like '村%' 7.消除一個字段的右邊的第一位: update tableName set [Title]=left([Title],(len([Title])-1)) where Title like '%村' 8.假刪除表中多余的重復記錄(多個字段),不包含rowid最小的記錄 update vitae set ispass=-1where peopleId in (select peopleId from vitae group by peopleId。
一句SQL查詢 要求去除重復語句
declare @tid nvarchar(50),@username nvarchar(50),@oldusername nvarchar(50),@str nvarchar(150),@sql nvarchar(max)
set @str=''
declare g_cursor cursor for
SELECT *,*me
FROM `pw_threads` AS t, pw_members AS u
WHERE *id = *
AND NOT isnull( * )
ORDER BY postdate DESC
open g_cursor
fetch next from g_cursor into @tid,@username
while @@FETCH_STATUS=0
begin
if @oldusername <> @username
set @str+=''+@tid+''+','
fetch next from g_cursor into @tid,@username
end
close g_cursor
deallocate g_cursor
if @str <> ''
begin
set @str=SUBSTRING(@str,1,LEN(@str)-1)
set @sql='select top 3 * from `pw_threads` AS t where tid in (@tid)'
exec @sql
end
幾個刪除重復記錄的SQL語句
百度一下,現成的啊,摘錄如下
用SQL語句,刪除掉重復項只保留一條
在幾千條記錄里,存在著些相同的記錄,如何能用SQL語句,刪除掉重復的呢
1、查找表中多余的重復記錄,重復記錄是根據單個字段(peopleId)來判斷
select * from people
where peopleId in (select peopleId from people group by peopleId having count(peopleId) >1)
2、刪除表中多余的重復記錄,重復記錄是根據單個字段(peopleId)來判斷,只留有rowid最小的記錄
delete from people
where peopleName in (select peopleName from people group by peopleName having count(peopleName) >1)
and peopleId not in (select min(peopleId) from people group by peopleName having count(peopleName)>1)
SQL語句,刪除重復記錄
如果按題目那個樣子,這個表只有一個字段的話樓上的解答應該是合要求的吧。
就是選出來存在一個臨時表里然后再填回去:select distinct test_name into #Tmp from test1 drop table test1select * into test1 from #Tmpdrop table #Tmp但是如果按正常的情況下這個表總會有個主鍵什么的吧?比如說是有個自增的ID字段。這種情況下就可以:delete test1 where ID not in (select ID from test1 group by test_name)。
SQL 語句去掉重復問題
SQL>delete cz where (id,forecid) in (select id,forecid from table group by id,forecid having count(*)>1) and rowid not in
(select min(rowid) from table group by id,forecid having count(*)>1);
SQL>delete table where rowid not in(select min(rowid) from cz group by id,forecid
這兩個方法都可以,適用于oracle刪除大量重復數據!