久久机这里只有精品,国产69精品一区二区亚洲孕妇,91精品国产综合久久婷婷香蕉,午夜久久久久久电影

最新活動(dòng):電腦PC端+手機(jī)端+微網(wǎng)站+自適應(yīng)網(wǎng)頁(yè)多模板選擇-建站388元起價(jià)?。?!
當(dāng)前位置:主頁(yè) > 網(wǎng)站建設(shè) > sql刪除重復(fù)數(shù)據(jù)的詳細(xì)方法建站知識(shí)

sql刪除重復(fù)數(shù)據(jù)的詳細(xì)方法建站知識(shí)

時(shí)間:2023-05-24 10:05:24 閱讀: 文章分類(lèi): 網(wǎng)站建設(shè) 作者: 網(wǎng)站編輯員

導(dǎo)讀:1建站知識(shí)重復(fù)數(shù)據(jù),通常有兩種:一是完全重復(fù)的記錄,也就是所有字段的值都一樣;二是部分字段值重復(fù)的記錄如何seo優(yōu)化推廣網(wǎng)站網(wǎng)站建設(shè)。

如何seo優(yōu)化推廣網(wǎng)站網(wǎng)站建設(shè)

一. 刪除完全重復(fù)的記錄

完全重復(fù)的數(shù)據(jù),通常是由于沒(méi)有設(shè)置主鍵/唯一鍵約束導(dǎo)致的。測(cè)試數(shù)據(jù):

復(fù)制代碼 代碼如下:

if OBJECT_ID('duplicate_all') is not nulldrop table duplicate_all GO create table duplicate_all ( c1 int, c2 int, c3 varchar(100) ) GO insert into duplicate_all select 1,100,'aaa' union allselect 1,100,'aaa' union allselect 1,100,'aaa' union allselect 1,100,'aaa' union allselect 1,100,'aaa' union allselect 2,200,'bbb' union allselect 3,300,'ccc' union allselect 4,400,'ddd' union allselect 5,500,'eee'GO

網(wǎng)站建設(shè)教程

(1) 借助臨時(shí)表

利用DISTINCT得到單條記錄,刪除源數(shù)據(jù),然后導(dǎo)回不重復(fù)記錄。如果表不大的話,可以把所有記錄導(dǎo)出一次,然后truncate表后再導(dǎo)回,這樣可以避免delete的日志操作。

復(fù)制代碼 代碼如下:

if OBJECT_ID('tempdb..#tmp') is not nulldrop table #tmp GO select distinct * into #tmp from duplicate_all where c1 = 1 GO delete duplicate_all where c1 = 1 GO insert into duplicate_all select * from #tmp

(2) 使用ROW_NUMBER

復(fù)制代碼 代碼如下:

with tmp as( select *,ROW_NUMBER() OVER(PARTITION BY c1,c2,c3 ORDER BY(getdate())) as num from duplicate_all where c1 = 1 ) delete tmp where num > 1

如果多個(gè)表有完全重復(fù)的行,可以考慮通過(guò)UNION將多個(gè)表聯(lián)合,插到一個(gè)新的同結(jié)構(gòu)的表,SQL Server會(huì)幫助去掉表和表之間的重復(fù)行。

二. 刪除部分重復(fù)的記錄

部分列重復(fù)的數(shù)據(jù),通常表上是有主鍵的,可能是程序邏輯造成了多行數(shù)據(jù)列值的重復(fù)。測(cè)試數(shù)據(jù):

復(fù)制代碼 代碼如下:

if OBJECT_ID('duplicate_col') is not nulldrop table duplicate_col GO create table duplicate_col ( c1 int primary key, c2 int, c3 varchar(100) ) GO insert into duplicate_col select 1,100,'aaa' union allselect 2,100,'aaa' union allselect 3,100,'aaa' union allselect 4,100,'aaa' union allselect 5,500,'eee'GO

(1) 唯一索引

唯一索引有個(gè)忽略重復(fù)建的選項(xiàng),在創(chuàng)建主鍵約束/唯一鍵約束時(shí)都可以使用這個(gè)索引選項(xiàng)。

復(fù)制代碼 代碼如下:

if OBJECT_ID('tmp') is not nulldrop table tmp GO create table tmp ( c1 int, c2 int, c3 varchar(100), constraint UQ_01 unique(c2,c3) with(IGNORE_DUP_建設(shè)網(wǎng)站公司KEY = ON) ) GO insert into tmp select * from duplicate_col select * from tmp

(2) 借助主鍵/唯一鍵來(lái)刪除通常會(huì)選擇主鍵/唯一鍵的最大/最小值保留,其他行刪除。以下只保留重復(fù)記錄中c1最小的行。

復(fù)制代碼 代碼如下:

delete from duplicate_col where exists(select 1 from duplicate_col b where duplicate_col.c1 > b.c1 and (duplicate_col.c2 = b.c2 and duplicate_col.c3 = b.c3))

關(guān)鍵詞標(biāo)簽: 標(biāo)題 標(biāo)簽

聲明: 本文由我的SEOUC技術(shù)文章主頁(yè)發(fā)布于:2023-05-24 ,文章sql刪除重復(fù)數(shù)據(jù)的詳細(xì)方法建站知識(shí)主要講述標(biāo)簽,標(biāo)題,sql刪除重復(fù)數(shù)據(jù)的詳細(xì)方法建站知識(shí)網(wǎng)站建設(shè)源碼以及服務(wù)器配置搭建相關(guān)技術(shù)文章。轉(zhuǎn)載請(qǐng)保留鏈接: http://www.bifwcx.com/article/web_6710.html

我的IDC 網(wǎng)站建設(shè)技術(shù)SEOUC.COM
專(zhuān)注網(wǎng)站建設(shè),SEO優(yōu)化,小程序設(shè)計(jì)制作搭建開(kāi)發(fā)定制網(wǎng)站等,數(shù)千家網(wǎng)站定制開(kāi)發(fā)案例,網(wǎng)站推廣技術(shù)服務(wù)。
  • 5000+合作客服
  • 8年從業(yè)經(jīng)驗(yàn)
  • 150+覆蓋行業(yè)
  • 最新熱門(mén)源碼技術(shù)文章

    主站蜘蛛池模板: 赣州市| 射阳县| 共和县| 南宫市| 商丘市| 台南县| 沧州市| 富裕县| 彩票| 文水县| 乌海市| 清原| 安阳市| 柏乡县| 上饶市| 京山县| 班玛县| 高唐县| 射阳县| 陆河县| 洮南市| 洞口县| 齐齐哈尔市| 菏泽市| 年辖:市辖区| 安陆市| 胶州市| 佛山市| 焦作市| 宜都市| 江源县| 卓资县| 离岛区| 洛南县| 英吉沙县| 尼玛县| 开江县| 德安县| 敦煌市| 江油市| 江都市|