主表:
輔表:
一個app對應多個apk,現在要取上線(Apk_Status最大的)的應用
select * from [dbo].[tbl_APP] as app join (select * from [dbo].[tbl_Apk] as AA where not exists(select top 1 * from tbl_Apk as BB where AA.Apk_APPId=BB.Apk_APPId and AA.Apk_Status < BB.Apk_Status)) as apk on app.APP_Id = apk.Apk_APPId
重點是取關聯的右表:
select * from [dbo].[tbl_Apk] as AA where not exists(select top 1 * from tbl_Apk as BB where AA.Apk_APPId=BB.Apk_APPId and AA.Apk_Status < BB.Apk_Status)
沒看懂這個not exists的邏輯 ~~