前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >mysql中not exists用法_not exists用法

mysql中not exists用法_not exists用法

作者头像
全栈程序员站长
发布2022-09-13 10:17:50
8.7K0
发布2022-09-13 10:17:50
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

not exists是sql中的一个语法,常用在子查询和主查询之间,用于条件判断,根据一个条件返回一个布尔值,从而来确定下一步操作如何进行,not exists也是exists或in的对立面。

45d34f5738dabf48726e1b1b0cc523ee.png
45d34f5738dabf48726e1b1b0cc523ee.png

not exists 是exists的对立面,所以要了解not exists的用法,我们首先了解下exists、in的区别和特点:

exists : 强调的是是否返回结果集,不要求知道返回什么, 比如:select name from student where sex = ‘m’ and mark exists(select 1 from grade where …)

只要exists引导的子句有结果集返回,那么exists这个条件就算成立了,大家注意返回的字段始终为1,如果改成“select 2 from grade where …”,那么返回的字段就是2,这个数字没有意义。所以exists子句不在乎返回什么,而是在乎是不是有结果集返回。

而 exists 与 in 最大的区别在于 in引导的子句只能返回一个字段,比如:select name from student where sex = ‘m’ and mark in (select 1,2,3 from grade where …)

in子句返回了三个字段,这是不正确的,exists子句是允许的,但in只允许有一个字段返回,在1,2,3中随便去了两个字段即可。

而not exists 和not in 分别是exists 和 in 的 对立面。exists (sql 返回结果集,为真)

主要看exists括号中的sql语句结果是否有结果,有结果:才会继续执行where条件;没结果:视为where条件不成立。not exists (sql 不返回结果集,为真)

主要看not exists括号中的sql语句是否有结果,无结果:才会继续执行where条件;有结果:视为where条件不成立。

not exists:经过测试,当子查询和主查询有关联条件时,相当于从主查询中去掉子查询的数据。

8c31ffc92793d9cc564798702616354a.png
8c31ffc92793d9cc564798702616354a.png

例如:

test数据:id name

1 张三

2 李四select * from test c where not exists

(select 1 from test t where t.id= ‘1’ )

–无结果select * from test c where not exists

(select 1 from test t where t.id= ‘1’ and t.id = c.id)

–返回2 李四

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/159845.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档