当前位置:首页 » 数据仓库 » 数据库中的notexists
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

数据库中的notexists

发布时间: 2022-10-02 04:55:51

1. 数据库语言关于not exist的用法

卤煮你好,
答案写法很好,我看了很久才弄明白,但我认为有漏洞,实际应用的话应该需要完善
我先说我的写法,再解释答案的逻辑
我的:
select st.sno, st.sname
FROM student st
where exists(
select 1 from SC a join Cource b on a.Cno=b.Cno where a.Sno=st.Sno and
a.Cno in(3,5,8) having count(*)=3
)
或者
select st.sno, st.sname
FROM student st
where exists(
select 1 from SC where Sno=st.Sno and
Cno in(3,5,8) having count(*)=3
)
/*通过和上面的比较你可以发现其实Cource其实没有作用,但是第一种写法更加严密,因为可以判断SC中的Cno是不是有效的*/
-------分割线--------------------------
再来看看这个答案
SELECT student.sno, student.sname
FROM student
WHERE not exists(select course.cno
from course
where course.cno in (3,5,8) and not exists(select *
from sc
where student.sno=sc.sno and course.cno=sc.cno));
看起来很复杂,我们先来拆分下
因为sql 的查询和执行是逐条进行的,主体是从Student表中中选数据,我们假设Student中有【小明】这个人,如何判断小明是不是该出来呢,只要
select course.cno
from course
where course.cno in (3,5,8) and not exists(select *
from sc
where ‘小明’=sc.sno and course.cno=sc.cno)
这一大坨【不返回结果】即可,
这一坨
,select course.cno
from course
where course.cno in (3,5,8) and not exists(select *
from sc
where ‘小明’=sc.sno and course.cno=sc.cno)
意思就比较明确了(我这儿迷糊了好一阵子)
只要小明同时选修了3,5,8那么这段话就不返回结果,所以最终小明就出现了!

/*ps.题目有个地方我没太看明白,“3且5且8” 是指的是同时选修3,5,8呢还是同时选修3,5,8且只选修这三个。如果是后者这3个写法还要再加一句exists*/
----三段写法全部测试通过,卤煮可以尽情测试~要给分啊!!!!!!!!!!!!

2. mysql数据库,not exists 语句

在sql语言里"存在"exists子句是非常不好理解的。

exists子句有两种用法,一种为独立exists子查询,另一种是父子关联子查询。前者对父查询不构成筛选作用,子查询若果有记录存在的话则输出所有的父查询记录集,反之则父查询输出空记录集。后者会对父查询构成筛选作用,不使用not关键字的情况下输出父查询中与子查询的交集,而使用not时则输出父查询中与子查询的非交集。至于如何判断exists子查询属于独立还是父子关联查询,以及为什么父子关联exists子查询会对父查询构成筛选作用,解释起来需要很大的篇幅这里就不讲了。反正我们记住父子关联查询的最常用功能就是它可以求出两张表的交集或非交集(使用not关键字)和不使用group分组的情况下求出某张表的最大值或最小值。

现在回到题主的具体问题上,这个问题涉及到三张表,学生表student、选课表sc、课程表course。

提问要求列出选取了所有课程的学生名单。
下面是提问中给出的sql语句:

select sname from student
where not exists(
select * from course
where not exists(
select * from sc
where sno=student.sno
and cno=course.cno));

从该语句我们看到它使用了两个嵌套父子关联不存在判断not exists子句,显然是要通过求非交集的方法查出选修了所有课程的学生名单。

一个学生如果他至少有一门课程没有选修,那么他在课程表里就会存在与选课表的非交集,我们姑且称之为“未选所有课程学生名单子集”,它由内层的not exists选出
...
select * from course
where not exists(
select * from sc
sno=student.sno
and cno=course.cno) .

3. sql数据库 not exists方法

select p.name from person p where not exists(
select 1 from b b left join a a on b.id = a.b_id
-- 这里是将所有a表中的数据作为查询项,可以改为a.id = 1
where a.id in (select id from a)

and b.p_id = p.id);

4. 关于数据库中select里的not exists

not exists 就相当于 not in
就是 不存在于 满足条件的 筛选结果中
但是存在于自身的表的数据

这个题目 你画三个圆圈 两两相交 用数学的交集差集理解下就明白了

5. 数据库问题exists 和not exists如何使用

exists和not exists我认为最大的特点就是可以使sql语句变得简单,一句话完成很复杂的操作,因为它本身具有循环的特点。
比如(b2是两个表的主键):
update tab1 as a set a.a1=b.a1 where exit(select b.a1 from tab2 as b where b.b2=a.b2);

上面的语句实现的功能是,根据b.b2=a.b2条件循环检索tab2表当检索出一个b.a1的值时,根据a.b2这个主键更新tab1表中相应的记录,当循环到exit后面没有结果产生时sql结束。
not exists 和exists 恰好相反。

※一定记住他和in、not in 是不一样的,他们是数据库内置的循环检索。

我知道的就这些希望对你有帮助。

6. 数据库中NOT EXISTS是什么意思

不在某些范围之内

你的这条语句就是说

查询不在这个范围之内的数据

7. sql中not exists用法

恩,对的,其实写法很多

1、
select id from a where id not in (select id_no from b)

2、
select id from a
except
select id_no from b

8. 怎么理解数据库中not exist

not exists 是取反逻辑,也就是里面查询没有结果集就是为真,如果有结果集就是为假。然后作为整体的条件拼接到着查询上

9. oracle中not exists 是什么意思

not exists就是检测有没有符合条件的记录的意思。
一般放到where后面,检测子查询的结果。