国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
    主站蜘蛛池模板: 东阿县| 新蔡县| 蒲城县| 图木舒克市| 通江县| 大姚县| 奉贤区| 久治县| 上蔡县| 屯昌县| 平原县| 固原市| 东兴市| 荣成市| 卢氏县| 神农架林区| 渑池县| 图们市| 三亚市| 佛山市| 弥渡县| 礼泉县| 若尔盖县| 蒲江县| 卢湾区| 潮安县| 锡林郭勒盟| 德惠市| 民和| 东兰县| 广宗县| 阜南县| 安远县| 洪湖市| 万源市| 宕昌县| 澳门| 许昌县| 应城市| 康保县| 治县。|