SQL一对多特殊查询,取唯一一条
转自:https://www.cnblogs.com/New-world/p/3866581.html
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的逻辑 ~~
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步