面試—尋找重複列

來源:互聯網
上載者:User
代碼

 1 尋找重複列:
 2 
 3 select * from ysgg where number in(select number from ysgg group by number having count(number)>1);
 4 
 5  
 6 
 7 重複資料刪除列:
 8 
 9 delete from ysgg where number in (select min(number) from ysgg group by number having count(number)>1);
10 

代碼

表stuinfo,有三個欄位recno(自增),stuid,stuname
  
  建該表的Sql語句如下:
  
  Create TABLE [StuInfo] (
   [recno] [int] IDENTITY (1, 1) NOT NULL ,
   [stuid] [varchar] (10) COLLATE Chinese_PRC_CI_AS NOT NULL ,
   [stuname] [varchar] (10) COLLATE Chinese_PRC_CI_AS NOT NULL
  ) ON [PRIMARY]
  GO
  
  1.--查某一列(或多列)的重複值(只能查出重複記錄的值,不能整個記錄的資訊)
  --如:尋找stuid,stuname重複的記錄
  select stuid,stuname from stuinfo
  group by stuid,stuname
  having(count(*))>1
  
  2.--查某一列有重複值的記錄(這種方法查出的是所有重複的記錄,也就是說如果有兩條記錄重複的,就查出兩條)
  --如:尋找stuid重複的記錄
  select * from stuinfo
  where stuid in (
  select stuid from stuinfo
  group by stuid
  having(count(*))>1
  )
  
  3.--查某一列有重複值的記錄(只顯示多餘的記錄,也就是說如果有三條記錄重複的,就顯示兩條)
  --這種方成績的前提是:需有一個不重複的列,本例中的是recno
  --如:尋找stuid重複的記錄
  select * from stuinfo s1
  where recno not in (
  select max(recno) from stuinfo s2
  where s1.stuid=s2.stuid
  )

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.