当前位置:Gxlcms > 数据库问题 > SQL一对多特殊查询,取唯一一条

SQL一对多特殊查询,取唯一一条

时间:2021-07-01 10:21:17 帮助过:3人阅读

SQL一对多特殊查询,取唯一一条

主表:

技术图片

辅表:

技术图片

一个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的逻辑 ~~

SQL一对多特殊查询,取唯一一条

标签:tps   logs   from   inf   特殊   info   pre   一个   HERE   

人气教程排行