In与Exists的区别
来源:互联网 发布:淘宝助理打开没有 编辑:程序博客网 时间:2024/05/29 10:27
http://www.cnblogs.com/eva_2010/articles/2047558.html
这两个函数是差不多的,但由于优化方案不同,通常NOT Exists要比NOT IN要快,因为NOT EXISTS可以使用结合算法而NOT IN就不行了,而EXISTS则不如IN快,因为这时候IN可能更多的使用结合算法。
Select * from tableA Where exists(Select * From tableB Where tableB.ID=tableA.ID)
这句相当于:Select * from tableA Where id in (Select ID From tableB)
对于表tableA的每一条数据,都执行Select * From tableB Where tableB.ID=tableA.ID的存在性判断,如果表tableB中存在表tableA当前行相同的ID,则Exists为真,该行显示,否则不显示。
- IN适合于外表大而内表小的情况;EXISTS适合于外表小而内表大的情况
- In确定给定的值是否与子查询或列表中的值相匹配
- Exists指定一个子查询,检测行的存在
-----IN 和NOT IN-----
----哪些部门中有雇员工资大于?-----
select * from Department where Department.deptID=
(select FDeptID from Employee where Employee.empSalary>7000)
----报错信息如下:子查询返回的值多于一个........
----Msg 512, Level 16, State 1, Line 1
----Subquery returned more than 1 value. This is not permitted when the
----subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.---
----改正:将“=”改为“in”
select * from Department where Department.deptID in
(select FDeptID from Employee where Employee.empSalary>7000)
go
----not in
select * from Department where Department.deptID in
(select FDeptID from Employee where Employee.empSalary>7000)
go
-----EXISTS 和NOT EXISTS-----一般用于IF语句的存在检测
----工资改革,检查雇员工资,达到以上的,每人提高,否则每人提高-----
/*--采用EXISTS子查询,进行酌情提升--*/
select * from Employee
go
if exists (select * from Employee where empSalary>7000)
begin
Print '有人工资达到,则每人提高,提高后工资为:'
update Employee set empSalary=empSalary+300
select * from Employee
end
else
begin
Print '无人工资达到,则每人提高,提高后工资为:'
update Employee set empSalary=empSalary+500
select * from Employee
end
go
----IN和Exists---
Select distinct deptName from Department
where exists(select * from Employee where empGender=1)
go
Select distinct deptName from Department
where deptID in(select FDeptID from Employee where empGender=1)
go
----exists相当于存在量词:表示集合存在,也就是集合不为空只作用于一个集合。
----exists P表示P不为空时为真;not Exists P表示P为空时,为真。
----in表示一个标量和医院关系的关系。s In P表示当s与P中的某个值相等时为真;
----s not in P表示s与P中的每一个值都不相等时,为真。
- EXISTS与IN的区别
- EXISTS与In的区别
- In与Exists的区别
- exists 与 in 的区别
- In与Exists的区别
- In与Exists的区别
- In与Exists的区别
- In与Exists的区别
- exists与in的区别
- oracle in与exists语句的区别
- sql中in与exists的区别
- ORACLE in与exists语句的区别
- SQL中EXISTS与IN的区别
- sql-exists与in的区别
- ORACLE in与exists语句的区别
- ORACLE in与exists语句的区别
- oracle中in与exists的区别
- not in与not exists的区别
- JBPortal数据同步
- Android--HttpURLConnection及HttpClient选择
- 第三十二讲|输出小星星
- HtmlHelper与扩展方法
- 《技术管理之巅:如何从零打造高质效互联网技术团队?》
- In与Exists的区别
- POJ 3264 Balanced Lineup(ST)
- 使用Ajax异步上传图片的方法(html,javascript,php)
- 数据结构与算法——插入类排序(直接插入排序,希尔排序)
- 【转载】职场人要远离的各种“负能量”
- mybatis 获取insert的id
- python入门读书笔记
- 每天一个linux命令(41):ps命令
- 第一个Python小程序