请求一个子查询的问题

hbgzg3006 2016-10-25 02:56:06
select *
FROM A a
WHERE a.update_time >= '20160101' and a.status_code is not null
and not exists (select 1 from (select 1 from E e where e.id = a.id order by e.update_time desc limit 1) t where e.status_code = a.status_code)

多一层子查询会发现找不到a.id,求教。
...全文
315 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
致命的西瓜 2016-10-26
  • 打赏
  • 举报
回复
引用 9 楼 hbgzg3006 的回复:
[quote=引用 7 楼 wangjian0228 的回复:] exists后返回的布尔值,你的select 1from... 即使返回值为空也是ture exists用法:
引用
http://dev.mysql.com/doc/refman/5.7/en/exists-and-not-exists-subqueries.html
为什么我测试的不是这样
select 1 from dual where EXISTS( select 1 from dual where 1 !=1)
[/quote] not exists
hbgzg3006 2016-10-25
  • 打赏
  • 举报
回复
引用 7 楼 wangjian0228 的回复:
exists后返回的布尔值,你的select 1from... 即使返回值为空也是ture exists用法:
引用
http://dev.mysql.com/doc/refman/5.7/en/exists-and-not-exists-subqueries.html
为什么我测试的不是这样
select 1 from dual where EXISTS( select 1 from dual where 1 !=1)
致命的西瓜 2016-10-25
  • 打赏
  • 举报
回复
外层调用只解析一层嵌套
致命的西瓜 2016-10-25
  • 打赏
  • 举报
回复
exists后返回的布尔值,你的select 1from... 即使返回值为空也是ture exists用法:
引用
http://dev.mysql.com/doc/refman/5.7/en/exists-and-not-exists-subqueries.html
hbgzg3006 2016-10-25
  • 打赏
  • 举报
回复
引用 5 楼 wangjian0228 的回复:
引用 4 楼 hbgzg3006 的回复:
这样是不满足要求的,因为只要有一个status_code相等 就能找出来,而我希望找的是最近的那个id相同的status_code是否相同
贴出表结构方便测试
大师,我就是想解决not exists里面两层循环 为啥找不到a的问题。
致命的西瓜 2016-10-25
  • 打赏
  • 举报
回复
引用 4 楼 hbgzg3006 的回复:
这样是不满足要求的,因为只要有一个status_code相等 就能找出来,而我希望找的是最近的那个id相同的status_code是否相同
贴出表结构方便测试
hbgzg3006 2016-10-25
  • 打赏
  • 举报
回复
引用 3 楼 wangjian0228 的回复:
自我解决问题能力满分
这样是不满足要求的,因为只要有一个status_code相等 就能找出来,而我希望找的是最近的那个id相同的status_code是否相同
致命的西瓜 2016-10-25
  • 打赏
  • 举报
回复
自我解决问题能力满分
hbgzg3006 2016-10-25
  • 打赏
  • 举报
回复
这样是没问题的
select * 
FROM A a
WHERE a.update_time >= '20160101' and a.status_code is not null
and not exists (select 1 from E e where e.id = a.id  and e.status_code = a.status_code order by e.update_time desc limit 1)
hbgzg3006 2016-10-25
  • 打赏
  • 举报
回复
抱歉写错了。
select * 
FROM A a
WHERE a.update_time >= '20160101' and a.status_code is not null
and not exists (select 1 from (select status_code from E e where e.id = a.id order by e.update_time desc limit 1) t where t.status_code = a.status_code)

56,677

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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