国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

如何刪除表中的重復記錄?

2024-07-21 02:05:57
字體:
供稿:網(wǎng)友
  • 本文來源于網(wǎng)頁設計愛好者web開發(fā)社區(qū)http://www.html.org.cn收集整理,歡迎訪問。

  • --測試數(shù)據(jù)
    /*-----------------------------
    select * from tt
    -----------------------------*/
    id          pid        
    ----------- -----------
    1           1
    1           1
    2           2
    3           3
    3           3
    3           3

    (所影響的行數(shù)為 6 行)

    首先,如何查詢table中有重復記錄
    select *,count(1) as rownum
    from tt
    group by id, pid
    having count(1) > 1
    id          pid         rownum     
    ----------- ----------- -----------
    1           1           2
    3           3           3

    (所影響的行數(shù)為 2 行)

    方法一:使用distinct和臨時表
    if object_id('tempdb..#tmp') is not null
    drop table #tmp
    select distinct * into #tmp from tt
    truncate table tt
    insert into tt select * from #tmp

    方法二:添加標識列
    alter table tt add newid int identity(1,1)
    go 
    delete from tt  where exists(select 1 from tt a where  a.newid>tt.newid and tt.id=a.id and tt.pid=a.pid)
    go
    alter table tt drop column newid
    go

    --測試結(jié)果
    /*-----------------------------
    select * from tt
    -----------------------------*/
    id          pid        
    ----------- -----------
    1           1
    2           2
    3           3

    (所影響的行數(shù)為 3 行)
    發(fā)表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發(fā)表
    主站蜘蛛池模板: 长乐市| 常熟市| 德昌县| 桐柏县| 达州市| 湖北省| 宝山区| 崇州市| 四子王旗| 板桥市| 固镇县| 修武县| 南澳县| 横山县| 玛曲县| 双流县| 侯马市| 奇台县| 扎兰屯市| 崇仁县| 景宁| 定州市| 潍坊市| 遂川县| 贺州市| 黄骅市| 云和县| 含山县| 海淀区| 凌源市| 湟中县| 汉寿县| 明光市| 礼泉县| 玛沁县| 承德市| 安阳市| 正安县| 石首市| 固安县| 师宗县|