in,any/some,all,exists (有信息返回1,没有返回0)
1.in(val1,val2,val3)
in 相当于要 查询的条件 = val1 or 查询的条件 = val1 ...
not in 不是in()里面的值
--查询有员工的部门名 查询出employees表中所有的部门id select DISTINCT department_id from employees; 再查询出部门表中的部门id于employees表中部门相同的部分 select department_name from departments where department_id in(select DISTINCT department_id from employees);
2.any/some 任一的
假设any内部的查询语句返回的结果个数是三个,如:result1,result2,result3 (result1 or result2 or result3)
语法如下:
select ...from ... where a > any(result1,result2,result3);
--返回其它工种中比job_id为‘IT_PROG’工种任一工资低的员工的员工号、姓名、job_id 以及salary select employee_id,last_name,job_id,salary from employees where salary<any( select DISTINCT salary from employees where job_id='IT_PROG' ) and job_id!='IT_PROG';
3.all 所有
--返回其它部门中比job_id为‘IT_PROG’部门所有工资都低的员工 的员工号、姓名、job_id 以及salary select employee_id,last_name,job_id,salary from employees where salary<all( select DISTINCT salary from employees where job_id='IT_PROG' ) and job_id!='IT_PROG';
4.exists
语法如下:
SELECT ... FROM table WHERE EXISTS (subquery)
该语法可以理解为:将主查询的数据,放到子查询中做条件验证,根据验证结果(TRUE 或 FALSE)来决定主查询的数据结果是否得以保留。
--查询有员工的部门名 当子表的department_id与主表中的department_id 相同时,返回true(1)反之为false(0)。为true的得以保留 select department_name from departments where exists ( select * from employees e where e.department_id = departments.department_id )
本文深入探讨了SQL中的in, any/some, all, exists等高级查询操作符的使用技巧。通过具体实例,讲解了如何利用这些操作符进行复杂条件筛选,提升查询效率。特别关注了不同操作符在处理多值比较、全量对比及存在性判断场景下的应用。
461

被折叠的 条评论
为什么被折叠?



