sql server中not in,in与not exists,exists有什么不同

2025-04-04 21:14:56
推荐回答(1个)
回答1:

我给你举几个例子你感受一下。

(1)select * from student where class not in ('1','2','3')

查询班级不在1,2,3的学生信息

(2))select * from student where class in ('1','2','3')

查询班级在1,2,3的学生信息

in和not in的用法,更多会出现在子查询中,

例如 select * from student where sno in (select sno from Exam where course ='English')  查询参加了英语考试的学生信息。

(3)exists 更多时候出现在if判断中, 它只做一个是或否的判断,例如如果存在birthday=今天的学生,那么就把他的age+1

if exists (select 1 from student where birthday=getdate())
BEGIN
update student set age=age+1 where birthday=getdate()
END
else 
select N'今天没有同学过生日'