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;
(1)sql查詢時去掉重復組擴展閱讀:
SQL (結構化查詢語言)是用於執行查詢的語法。在資料庫上執行的大部分工作都由 SQL 語句完成。SQL 語言包含用於更新、插入和刪除記錄的語法。
增刪改查指令構成了 SQL 的 DML 部分:
SELECT- 從資料庫表中獲取數據
UPDATE- 更新資料庫表中的數據
DELETE- 從資料庫表中刪除數據
INSERT INTO- 向資料庫表中插入數據
② 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
(2)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)
③ mysql查詢去掉重復數據
可以利用distinct關鍵字對需要處理的欄位進行去重
④ SQL語句查詢 如何刪除重復多餘的數據
這個是SQL中distinct的典型用法:
1)從字面意思就可以了解到:
distinct
[dis'tiŋkt]
adj.
明顯的;獨特的;清楚的;有區別的
2)在SQL中用distinct來消除重復出現的欄位值。
使得每個欄位值只出現一次。
具體用法如下:
select
distinct
欄位名
from
表;
distinct
欄位名
意思就是只顯示一次該欄位名
一般情況下和order
by
結合使用,這樣可以提高效率。
所以這個問題的答案是:select
distinct
1,2,3,4
from
表;
1,2,3,4分別代表第一,二,三,四列的欄位名,我猜測可能第一列就是每個人的ID,
這樣你把重復的ID過濾留下一個,估計就是你想要的結果了。
希望我的回答能讓您滿意。
⑤ 在sql語言中去掉重復值的命令是
distinct。
SQLserver中很明顯的去重復的語句是distinct。selectdistinct是去除重復的記錄行,count(distinctColumn),消除重復值。還有一些不明顯的具有去重功能的詞,例如union,會去除重復的記錄行或值。
⑥ 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
(6)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 查詢時如何去掉重復的部分寫在where中
你說的重復是指兩條記錄完成一樣么,如果是則用
SELECT DISTINCT COLUMN1,COLUMN2
FROM TABLE1
或
SELECT COLUMN1,COLUMN2
FROM TABLE1
GROUP BY COLUMN1,COLUMN2
若你要查詢出來的欄位在兩條記錄中有不同值,可以用MAX()
或者可以根據你自己的要求進行排重,如下:
SELECT *
FROM (
SELECT A.*,ROW_NUMBER() OVER(PARTITION BY ID ORDER BY ID) RN
--按ID進行歸類,ORDER BY 按你自己的需要確定怎 樣的記錄在前面,怎樣的記錄在後面,因為最後的限制條件是RN = 1
FROM TABLE1 A
)
WHERE RN = 1