数据库语言 not exists 是什么意思

数据库语言 not exists 是什么意思,第1张

就是不在这个范围的意思

select id

from table

where name not exists(select 1

from table

where name = 'aaa')

也许你看不懂这个1的意思,不过在使用EXISTS的时候,通常都会使用1,它代表所查询出来的集合,等同于select name。

整个例子的意思是:查询name不等于aaa的所有ID。

不明白再问我,谢谢!

卤煮你好,

答案写法很好,我看了很久才弄明白,但我认为有漏洞,实际应用的话应该需要完善

我先说我的写法,再解释答案的逻辑

我的:

select stsno, stsname

FROM student st

where exists(

select 1 from SC a join Cource b on aCno=bCno where aSno=stSno and

aCno in(3,5,8) having count()=3

)

或者

select stsno, stsname

FROM student st

where exists(

select 1 from SC where Sno=stSno and

Cno in(3,5,8) having count()=3

)

/通过和上面的比较你可以发现其实Cource其实没有作用,但是第一种写法更加严密,因为可以判断SC中的Cno是不是有效的/

-------分割线--------------------------

再来看看这个答案

SELECT studentsno, studentsname

FROM student

WHERE not exists(select coursecno

from course

where coursecno in (3,5,8) and not exists(select

from sc

where studentsno=scsno and coursecno=sccno));

看起来很复杂,我们先来拆分下

因为SQL 的查询和执行是逐条进行的,主体是从Student表中中选数据,我们假设Student中有小明这个人,如何判断小明是不是该出来呢,只要

select coursecno

from course

where coursecno in (3,5,8) and not exists(select

from sc

where ‘小明’=scsno and coursecno=sccno)

这一大坨不返回结果即可,

这一坨

,select coursecno

from course

where coursecno in (3,5,8) and not exists(select

from sc

where ‘小明’=scsno and coursecno=sccno)

意思就比较明确了(我这儿迷糊了好一阵子)

只要小明同时选修了3,5,8那么这段话就不返回结果,所以最终小明就出现了!

/ps题目有个地方我没太看明白,“3且5且8” 是指的是同时选修3,5,8呢还是同时选修3,5,8且只选修这三个。如果是后者这3个写法还要再加一句exists/

----三段写法全部测试通过,卤煮可以尽情测试~要给分啊!!!!!!!!!!!!

以上就是关于数据库语言 not exists 是什么意思全部的内容,包括:数据库语言 not exists 是什么意思、数据库语言关于not exist的用法、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/9853145.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-02
下一篇2023-05-02

发表评论

登录后才能评论

评论列表(0条)

    保存