❶ 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
a.rowid
from
t
a,t
b
where
a.col1=b.col1
and
a.col2
=
b.col2
and
a.rowid
>
b.rowid)
######################################
10.
刪除重復記錄
最高效的刪除重復記錄方法
(
因為使用了rowid)
delete
from
emp
e
where
e.rowid
>
(select
min(x.rowid)
from
emp
x
where
x.emp_no
=
e.emp_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語句刪除重復的記錄
刪除重復的數據
delete from tb where id not in (
select id from
(select fileSize,fileName ,max(id) id from tb group by filesize,filename ) a
)
現在完成了重復數據的刪除,主要是利用了找出某個分組中最大的那個id,其中包括了所有不重復的id,然後使用not in將需要保留的排除。
❸ 怎麼樣刪除SQL SERVER 資料庫中重復的數據
1、必須保證表中有主鍵或者唯一索引,或者某列數據不能重復。只有這樣,才可能使用一句SQL來實現。否則只能考慮其它辦法。下面的語句,假定BB列是不重復的,刪除後保存BB列值最大的那條記錄。
delete
from
表
where
aa
in
(select
aa
from
表
group
by
aa
having
count(aa)
>
1)
and
bb
not
in
(select
max(bb)
from
表
group
by
aa
having
count(aa)
>
1);
2、有多種寫法:
delete
A
from
B
where
A.AA
=
B.AA
delete
A
from
A,B
where
A.AA
=
B.AA
delete
A
where
AA
in
(select
AA
from
B)
3、使用into關鍵字:
select
*
into
新表名
from
原表
4、取數據前3位,欄位必須是類似char類型,使用類似substring這樣的函數(SYBASE是substring,ORACLE是substr):
select
substring(欄位,1,3)
from
表名
❹ sql資料庫中出現重復行數據,如何刪除這些重復記錄
示例
假設存在一個產品信息表Procts,其表結構如下:
CREATETABLEProcts(
ProctIDint,
ProctNamenvarchar(40),
Unitchar(2),
UnitPricemoney
)
表中數據如圖:
*fromProcts_tempdroptableProcts_temp
這樣就完成了對表中重復記錄的刪除。無論表有多大,它的執行速度都是相當快的,而且因為幾乎不用寫語句,所以它也是很安全的
❺ sql查詢去掉重復記錄
1、打開要去掉重復數據的資料庫,這里新建一張含有重復數據的user表做示例,如下圖所示:
❻ mysql怎麼去除重復數據
MySQL 刪除重復數據
有些 MySQL 數據表中可能存在重復的記錄,有些情況我們允許重復數據的存在,但有時候我們也需要刪除這些重復的數據。
本章節我們將為大家介紹如何防止數據表出現重復數據及如何刪除數據表中的重復數據。
刪除重復數據
如果你想刪除數據表中的重復數據,你可以使用以下的SQL語句:
❼ 如何刪除sql表的索引重復值
例,B1表中ID應為主鍵,但建表時未設,表中有重復,刪除語句如下:
select id,max(c1) as c1,max(c2) as c2,... into #temptable from b1 group by id
go
delete from b1
go
insert into b1 select * from #temptable
go
drop table #temptable
go
❽ sql server 怎麼刪除表裡重復數據
1、必須保證表中有主鍵或者唯一索引,或者某列數據不能重復。只有這樣,才可能使用一句SQL來實現。否則只能考慮其它辦法。下面的語句,假定BB列是不重復的,刪除後保存BB列值最大的那條記錄。
delete from 表
where aa in (select aa from 表 group by aa having count(aa) > 1) and
bb not in (select max(bb) from 表 group by aa having count(aa) > 1);
2、有多種寫法:
delete A from B where A.AA = B.AA
delete A from A,B where A.AA = B.AA
delete A where AA in (select AA from B)
3、使用into關鍵字:
select * into 新表名 from 原表
4、取數據前3位,欄位必須是類似char類型,使用類似substring這樣的函數(SYBASE是substring,ORACLE是substr):
select substring(欄位,1,3) from 表名
❾ SQL中如何刪除重復數據
select
欄位1,欄位2,欄位3
from
table
group
by
欄位1,欄位2,欄位3
having
count(*)>1
用上邊這句能找出所有重復的數據
欄位1,2,3你替換成你表裡的欄位名,如果有更多欄位的話,你就繼續添加,最後group
by的時候不要忘記了
刪除的時候要建立一個臨時表
create
table
new_table
as
select
欄位1,欄位2,欄位3
from
old_table
group
by
欄位1,欄位2,欄位3;
然後刪除原表數據
truncate
table
old_table;
然後把臨時表數據反插回去
insert
into
new_table
select
*
from
old_table;
❿ 怎麼刪除SQL中重復的記錄
嘗試回答LZ的問題,僅供參考:
1、對於這種要求,必須保證表中有主鍵或者唯一索引,或者某列數據不能重復。只有這樣,才可能使用一句SQL來實現。否則只能考慮其它辦法。下面的語句,假定BB列是不重復的,刪除後保存BB列值最大的那條記錄。
delete from 表
where aa in (select aa from 表 group by aa having count(aa) 1) and
bb not in (select max(bb) from 表 group by aa having count(aa) 1);
2、有多種寫法: