<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    少年阿賓

    那些青春的歲月

      BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
      500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks

    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(*)>1)

    (二)
    比方說
    在A表中存在一個字段“name”,
    而且不同記錄之間的“name”值有可能會相同,
    現在就是需要查詢出在該表中的各記錄之間,“name”值存在重復的項;
    Select Name,Count(*) From A Group By Name Having Count(*) > 1

    如果還查性別也相同大則如下:
    Select Name,sex,Count(*) From A Group By Name,sex Having Count(*) > 1

    (三)
    方法一

    declare @max integer,@id integer

    declare cur_rows cursor local for select 主字段,count(*) from 表名 group by 主字段 having count(*) >; 1

    open cur_rows

    fetch cur_rows into @id,@max

    while @@fetch_status=0

    begin

    select @max = @max -1

    set rowcount @max

    delete from 表名 where 主字段 = @id

    fetch cur_rows into @id,@max
    end

    close cur_rows

    set rowcount 0

    方法二

    "重復記錄"有兩個意義上的重復記錄,一是完全重復的記錄,也即所有字段均重復的記錄,二是部分關鍵字段重復的記錄,比如Name字段重復,而其他字段不一定重復或都重復可以忽略。

    1、對于第一種重復,比較容易解決,使用

    select distinct * from tableName

    就可以得到無重復記錄的結果集。

    如果該表需要刪除重復的記錄(重復記錄保留1條),可以按以下方法刪除

    select distinct * into #Tmp from tableName

    drop table tableName

    select * into tableName from #Tmp
    drop table #Tmp

    發生這種重復的原因是表設計不周產生的,增加唯一索引列即可解決。

    2、這類重復問題通常要求保留重復記錄中的第一條記錄,操作方法如下

    假設有重復的字段為Name,Address,要求得到這兩個字段唯一的結果集

    select identity(int,1,1) as autoID, * into #Tmp from tableName

    select min(autoID) as autoID into #Tmp2 from #Tmp group by Name,autoID

    select * from #Tmp where autoID in(select autoID from #tmp2)

    最后一個select即得到了Name,Address不重復的結果集(但多了一個autoID字段,實際寫時可以寫在select子句中省去此列)

    (四)
    查詢重復

    select * from tablename where id in (select id from tablename

    group by id

    having count(id) > 1

    )

    3、查找表中多余的重復記錄(多個字段)
    select * from vitae a
    where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)

    運行會產生問題,where(a.peopleId,a.seq)這樣的寫發是通不過的!!!

    posted on 2012-11-21 23:20 abin 閱讀(545) 評論(0)  編輯  收藏 所屬分類: Database
    主站蜘蛛池模板: 亚洲综合国产精品| 亚洲福利在线视频| 亚洲精品色在线网站| 女人与禽交视频免费看| 中文字幕亚洲情99在线| 成年轻人网站色免费看| 国产亚洲福利在线视频| 卡一卡二卡三在线入口免费| 亚洲最大天堂无码精品区| 好吊妞在线成人免费| 亚洲AV成人无码久久WWW| 在线观看国产情趣免费视频| 精品国产亚洲AV麻豆| 亚洲XX00视频| 国色精品va在线观看免费视频| 亚洲精品国偷自产在线| 99精品视频在线观看免费播放| 亚洲黄网在线观看| 色妞WWW精品免费视频 | 四虎永久免费影院| 四虎成人精品国产永久免费无码| 亚洲成人国产精品| 美女视频黄的免费视频网页 | 国产又黄又爽胸又大免费视频| 亚洲精品国产品国语在线| 亚洲黄色片免费看| 亚洲av无码偷拍在线观看| 中文字幕无码精品亚洲资源网| 久久久久久AV无码免费网站下载| 亚洲成人午夜电影| 亚洲AⅤ无码一区二区三区在线 | 亚洲熟女乱综合一区二区| 久久久久久久岛国免费播放| 亚洲va精品中文字幕| www亚洲一级视频com| 一级成人a毛片免费播放| 亚洲1区2区3区精华液| 亚洲av伊人久久综合密臀性色| 亚洲天堂免费在线| 国产99视频精品免费视频76| 亚洲最新中文字幕|