sql查詢去除重復值語句
sql 單表/多表查詢去除重復記錄
單表distinct
多表group by
group by 必須放在 order by 和 limit之前,不然會報錯
************************************************************************************
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 peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )>1)
3、查找表中多餘的重復記錄(多個欄位)
select * from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
4、刪除表中多餘的重復記錄(多個欄位),只留有rowid最小的記錄
delete from vitae a
where (a.peopleId,a.seq) 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 (a.peopleId,a.seq) 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(*)>
㈡ SQL查詢,如何去除重復的記錄
首先,先說明一個問題。這樣的結果出現,說明系統設計是有問題的。
其次
刪除重復數據,你要提供你是什麼資料庫。
不同資料庫會有不同的解決方案。
關鍵字Distinct 去除重復,如下列SQL,去除Test相同的記錄;
1. select distinct Test from Table
2. 如果是要刪除表中存在的重復記錄,那就邏輯處理,如下:
3. select Test from Table group by Test having count(test)>1
4. 先查詢存在重復的數據,後面根據條件刪除
還有一個更簡單的方法可以嘗試一下:
select aid, count(distinct uid) from 表名 group by aid
這是sqlserver 的寫法。
如圖一在數據表中有兩個膀胱沖洗重復的記錄。
㈢ 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 (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
4、刪除表中多餘的重復記錄(多個欄位),只留有rowid最小的記錄
delete from vitae a
where (a.peopleId,a.seq) 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 (a.peopleId,a.seq) 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=-1
where peopleId in (select peopleId from vitae group by peopleId
㈣ sql中怎麼刪除兩條重復記錄並保留一條
將數據去重復後暫存到臨時表#a中
selectdistinct*into#afromtable1where條件
deletetable1where刪除限制條件
insertintotable1select*from#a-將暫存的數據插回資料庫
droptable#a-刪除臨時表
註:當前的資料庫,每一個表都應該有一個標志欄位,以保證記錄不完全重復,否則實用中極易出問題。
(4)sql查找刪除多條件重復記錄擴展閱讀:
SQL語句刪除掉重復的其他情況
1、查找表中多餘的重復記錄,重復記錄是根據單個欄位(peopleId)來判斷
SELECT
*
FROM
people
WHERE
peopleId IN (
SELECT
peopleId
FROM
people
GROUP BY
peopleId
HAVING
count(peopleId) > 1
)
2、查找表中多餘的重復記錄(多個欄位)
SELECT
*
FROM
vitae a
WHERE
(a.peopleId, a.seq) IN (
SELECT
peopleId,
seq
FROM
vitae
GROUP BY
peopleId,
seq
HAVING
count(*) > 1
)
參考資料來源:結構化查詢語言(SQL)-網路
㈤ SQL怎樣刪除重復數據
首先刪除一張表中可能存在的重復數據:x0dx0adelete from 表 where 欄位1 inx0dx0a(select 欄位1 from x0dx0a (select 欄位1,row_number() over (partition by 欄位1 order by 欄位2 desc) rn from 表)x0dx0awhere rn>1);x0dx0a以上欄位1為需要刪除的依據欄位,比如說你需要刪除重復的郵箱,那麼欄位1表示郵箱,而欄位2是按照順序你需要保留的記錄,比如說按照時間排序,保留時間最近的那個郵箱。x0dx0ax0dx0a刪除一張表中的另一個表中已經存在的記錄x0dx0adelete from 表1 where existsx0dx0a(selete 1 from 表2 where 表1.欄位=表2.欄位);
㈥ sql中如何刪除一個表中重復的記錄
sql中刪除一個表中的重復記錄可以採用如下步驟:
1、把a_dist表的記錄用distinct去重,結果放到臨時表中。
select distinct * into #temp from a_dist;
2、把a_dist表的記錄全部刪除。
delete from a_dist;
3、把臨時表中的數據信息導進到a_dist表中,並刪除臨時表。
insert into a_distselect * from #temp;
drop table #temp;
(6)sql查找刪除多條件重復記錄擴展閱讀:
SQL (結構化查詢語言)是用於執行查詢的語法。在資料庫上執行的大部分工作都由 SQL 語句完成。SQL 語言包含用於更新、插入和刪除記錄的語法。
增刪改查指令構成了 SQL 的 DML 部分:
SELECT- 從資料庫表中獲取數據
UPDATE- 更新資料庫表中的數據
DELETE- 從資料庫表中刪除數據
INSERT INTO- 向資料庫表中插入數據
㈦ SQL查詢中如何剔除重復
1,存在兩條完全相同的紀錄
這是最簡單的一種情況,用關鍵字distinct就可以去掉
example: select distinct * from table(表名) where (條件)
2,存在部分欄位相同的紀錄(有主鍵id即唯一鍵)
如果是這種情況的話用distinct是過濾不了的,這就要用到主鍵id的唯一性特點及group by分組
example:
select * from table where id in (select max(id) from table group by [去除重復的欄位名列表,....])
3,沒有唯一鍵ID
example:
select identity(int1,1) as id,* into newtable(臨時表) from table
select * from newtable where id in (select max(id) from newtable group by [去除重復的欄位名列表,....])
drop table newtable
(7)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 peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )>1)
3、查找表中多餘的重復記錄(多個欄位)
select * from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
㈧ SQL查詢中如何剔除重復
1、存在部分欄位相同的紀錄
如果是這種情況的話用distinct是過濾不了的,這就要用到主鍵id的唯一性特點及group
代碼:select
*
from
table
where
id
in
(select
max(id)
from
table
group
by
[去除重復的欄位名列表,....])
2、存在兩條完全相同的記錄
這是最簡單的一種情況,用關鍵字distinct就可以去掉
代碼:select
distinct
*
from
table(表名)
where
(條件)
3、沒有唯一鍵ID
這種較為復雜
代碼:
select
identity(int1,1)
as
id,*
into
newtable(臨時表)
from
table(原表)
select
*
from
newtable
where
id
in
(select
max(id)
from
newtable
group
by
[去除重復的欄位名列表,....])
drop
table
newtable
(8)sql查找刪除多條件重復記錄擴展閱讀:
SQL查詢語句
1、查詢全部的重復信息
select
*
from
people
where
id
not
in
(
select
min(id)
from
people
group
by
name,sex
HAVING
COUNT(*)
<
2)
2、查詢多餘的重復信息
select
*
from
people
where
id
not
in
(
select
MIN(id)
from
people
group
by
name,sex)
㈨ 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將需要保留的排除。