SQL学习 暑假第四天

来源:互联网 发布:微博好看的域名 编辑:程序博客网 时间:2024/06/06 16:36

 

1. 子查询

   select * from ...  where    ...  in  select .. from ...                   

 子查询  就是where后面接着 select 语句  例子:

 

select * from 辅导员信息 A  where  A.辅导员编号  IN( select B.辅导员  from 班级信息 B)                           --IN()   要加括号

 

2.  EXISTS()    NOT EXISTS() 关键字查询     用在where后判断 select 语句是否返回了结果 

 

在用户登录系统这个 EXISTS()语句很有用例如 

 

if  EXISTS(Select * from  user where  user ID='232323' AND password='2rewfew' )          在用户登录系统经常用到

 

declare  @username  nchar(20);
set @username ='王艳';
select * from 班级信息 where   EXISTS( select * from 辅导员信息 A where  A.姓名=@username)

 

3.

--嵌套子查询    查询中使用子查询 子查询中嵌套使用 子查询
select * from  辅导员信息  A where A.辅导员编号 IN (select B.辅导员   from 班级信息 B  )

 

原创粉丝点击