关于where not exists(select 1 from ta)的问题

野鼻孔 2016-03-24 11:38:22
求大神帮忙捋一捋逻辑。
比如说我想实现。 申请号相同,更新号最大的Serial_number, 我这比对的两张表是相同的。
代码如下:
select Serial_Number,Diary_Type,Update_Number,Application_Number from
(SELECT Serial_Number,Diary_Type,Update_Number,Application_Number FROM A_Diary_Notes(nolock) ) a
where exists (select 1 from
(SELECT Serial_Number,Diary_Type,Update_Number,Application_Number FROM A_Diary_Notes(nolock) ) b
where a.application_number = b.application_number and a.Update_Number > b.Update_Number )


我想问一下以上代码是否正确?
如果我用where not exists(select 1 from ...... 那么之后的where 应该怎么对应更改呢? 是不是在这种情况下无法写成where not exists的形式?

求大神详细解答!!
...全文
1180 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
tcmakebest 2016-03-25
  • 打赏
  • 举报
回复
要取最大值自然有 max 函数, 不用这么拐个弯啊, 要问更新的问题,别只拿出一个where语句.
野鼻孔 2016-03-25
  • 打赏
  • 举报
回复
我在跑 这条语句的时候 select Serial_Number,Diary_Type,Update_Number,Application_Number from (SELECT Serial_Number,Diary_Type,Update_Number,Application_Number FROM A_Diary_Notes(nolock) where Diary_Type='M') a where not exists (select 1 from (SELECT Serial_Number,Diary_Type,Update_Number,Application_Number FROM A_Diary_Notes(nolock) where Diary_Type='M') b where a.application_number = b.application_number and a.Update_Number < b.Update_Number 得出结果 Serial_Number Diary_Type Update_Number Application_Number 104871 M 0 120000 104869 M 0 121204 104870 M 0 121204 19020 M 8 0728935 19022 M 8 0728935 104749 M 0 1000177264 104750 M 0 1000177264 104751 M 0 1000177264 104752 M 0 1000177278 104753 M 0 1000177278 104831 M 6 1000182096 104856 M 6 1000182096 104578 M 2 S131217039 104579 M 2 S131217039 104580 M 2 S131217039 104857 M 4 1000182103 104858 M 4 1000182103 我想再写个条件,当 application_number相同时,取最大的一条Serial_number
野鼻孔 2016-03-25
  • 打赏
  • 举报
回复
引用 4 楼 tcmakebest 的回复:
要取最大值自然有 max 函数, 不用这么拐个弯啊, 要问更新的问题,别只拿出一个where语句.
比如??求简单代码说明
yooq_csdn 2016-03-24
  • 打赏
  • 举报
回复
拿几行数据当例子吧
LongRui888 2016-03-24
  • 打赏
  • 举报
回复
其实这种子查询的语句,你可以这么来想 exists,就是说 你外面的表里的数据,在 exists(...) 这里也是存在的,那么外面的表数据就返回 而not exists就是相反的,当外面的数据,在里面没有,也就是不存在的时候,就返回外面的数据。

27,581

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧