mysql exists及not exists的使用

对exists及not exists的使用根据下面的示例进行解释

如sql:

select sname from student where exists (select * from score));

这里,如果select * from score语句查询到结果【只要不是Empty,就是查询到了结果】,则当执行exists判断的时候就会返回true,如果结果集为空,则exists判断结果为false。同理,not exists与exists判断结果相反。

exists一般用于内联查询如下:

表结构如图,查找任课教师中,没被匡明选过课的教师编号和姓名[编写sql查询]

select a.tno 教师编号,a.tname 教师姓名 from teacher a 
where exists(select *
                 from student b,score c ,course d 
                        where b.sno = c.sno and d.cno = c.cno and b.
                        sname='匡明' and d.tno = a.tno
                    );
原文地址:https://www.cnblogs.com/sunshinekevin/p/9105593.html