[SQL]一個刪選資料的例子,使用GROUP、DISTINCT

來源:互聯網
上載者:User

今天遇到的問題,糾結了一上午,終於解決了。在此記錄下來,自我認為還有很多類似的問題都可以套用這段代碼。


需求描述:

一個表MyImage,列有:號碼ID,路徑PATH
如:

ID  PATH

1  C:/
1  C:/
1  D:/
2  C:/
2  C:/
3  A:/
3  C:/
4  D:/



寫個SQL語句,返回這樣的記錄的ID號:相同ID存在不同PATH。如上例子,正確的結果應是:

ID

1

3

(ID2沒有不同PATH,ID4隻有一條記錄不存在不同PATH)


這是我的SQL語句:

select ID from [MyImage] as A  group by ID  having   (  select COUNT(distinct(Path)) from [MyImage] as B where B.ID = A.ID  )  > 1


一上午,就寫出了這麼一句啊!精華!有木有!

相關文章

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.