MySql的回顾六:子查询(内查询)

  西北望乡何处是,东南见月几回圆。

       月亮又慢悠悠的挂上了天空,趁着睡前梦呓,我就带领各位可爱的读者们探索MySql最后的子查询部分。

      说明:有些查询结果出来结果截图与题目要求不一样会出现多余的字段是为了方便展示结果的可读性。实际操作的读者可以删除SELECT后面多余的字段得到正确的结果。

#WHERE或HAVING后面#1.标量子查询(单行子查询)#2.列子查询(多行子查询)#3.行子查询(多列多行)#特点:# ①子查询放在小括号内# ②子查询一般放在条件的右侧# ③标量子查询:一般搭配着单行操作符使用#  单行操作符: > < >= <= <> !-#  列子查询,一般搭配着多行操作符使用#  IN,ANY/SOME(任意),ALL# ④子查询的执行优先与主查询执行,主查询的条件用到了子查询的结果。
#1.标量子查询#案例1:谁的工资比Abel高?#①查询Abel的工资SELECT salaryFROM employeesWHERE last_name = 'Abel';

#②查询员工的信息,满足Salary>①结果SELECT *FROM employeesWHERE salary>(SELECT salary FROM employees WHERE last_name='Abel');

#案例2.返回job_id与141号员工相同,salary比143号员工多的员工姓名,job_id,工资。#①查141员工的job_idSELECT job_idFROM employeesWHERE employee_id='141';

#②查143员工的salarySELECT salaryFROM employeesWHERE employee_id='143';

#③最后合并结果SELECT CONCAT(last_name,first_name) AS 姓名,job_id AS 工种编号,salary AS 工资FROM employeesWHERE job_id=( SELECT job_id FROM employees WHERE employee_id='141')AND salary>( SELECT salary FROM employees WHERE employee_id='143');  

#案例3.返回公司工资最少的员工的last_name,job_id和salary。SELECT MIN(salary)FROM employees;

SELECTlast_name AS 姓,salary AS 工资,job_id AS 工种编号FROM employeesWHERE salary=( SELECT MIN(salary) FROM employees     );

#案例4.查询最低工资大于50号部门最低工资的部门id和其最低工资。#①查50部门的最低工资SELECT MIN(salary)FROM employeesWHERE department_id=50;

#分组后,筛选条件①.【不用排除没有部门的所以不筛选部门编号】SELECT department_id AS 部门编号,MIN(salary) AS 月薪FROM employees#WHERE department_idGROUP BY department_idHAVING 月薪>( SELECT MIN(salary) FROM employees    );

#2.列子查询(多行子查询)#返回多行#使用多行比较操作符

#案例1.返回location_id是1400或1700的部门中的所有员工姓名。#①查询location_id是1400或1700的部门编号SELECT DISTINCT department_idFROM departmentsWHERE location_id IN(1400,1700);

#②查询员工姓名,要求部门号是①列表的某一个SELECT CONCAT(last_name,first_name) AS 姓名FROM employeesWHERE department_id IN ( SELECT DISTINCT department_id FROM departments WHERE location_id IN(1400,1700) );

用ANY替代IN与上面同样的结果SELECT CONCAT(last_name,first_name) AS 姓名FROM employeesWHERE department_id = ANY( SELECT DISTINCT department_id FROM departments WHERE location_id IN(1400,1700) );
#案例.返回location_id不是1400或1700的部门中的所有员工姓名。SELECT CONCAT(last_name,first_name) AS 姓名FROM employeesWHERE department_id NOT IN( SELECT DISTINCT department_id FROM departments WHERE location_id IN(1400,1700));==============================SELECT CONCAT(last_name,first_name) AS 姓名FROM employeesWHERE department_id <> ALL( SELECT DISTINCT department_id FROM departments WHERE location_id IN(1400,1700));

#案例2.返回其他工种中比job_id为IT_PROG部门任意一工资低的员工工号,# 姓名,job_id以及salary#①把IT_PROG部门中的工资查出来SELECT DISTINCT salaryFROM employeesWHERE job_id='IT_PROG';

#②把不是IT_PROG部门信息查出来SELECT *FROM employeesWHERE job_id != 'IT_PROG';

#③合并①与②在员工表中查出来SELECT employee_id AS 员工编号,CONCAT(last_name,first_name) AS 姓名,job_id AS 工种编号,salary AS 工资FROM employeesWHERE job_id != 'IT_PROG'AND salary<ANY( SELECT salary FROM employees WHERE job_id='IT_PROG'  );

用MAX代替ANY与上面同样的效果SELECT employee_id AS 员工编号,CONCAT(last_name,first_name) AS 姓名,job_id AS 工种编号,salary AS 工资FROM employeesWHERE job_id <> 'IT_PROG'AND salary<( SELECT MAX(salary) FROM employees WHERE job_id='IT_PROG'  );
#案例3.返回其他部门中比job_id为‘IT_PROG’部门所有工资都低的员工#的员工号,姓名,job_id以及salary。#①先把IT_PROG部门的工资查出来。SELECT DISTINCT salaryFROM employeesWHERE job_id='IT_PROG';

SELECT employee_id AS 员工号,CONCAT(last_name,first_name) AS 姓名,job_id AS 工种编号,salary AS 工资FROM employeesWHERE salary<ALL( SELECT DISTINCT salary FROM employees WHERE job_id='IT_PROG') AND job_id <> 'IT_PROG';=============================MIN替代ALLSELECT employee_id AS 员工号,CONCAT(last_name,first_name) AS 姓名,job_id AS 工种编号,salary AS 工资FROM employeesWHERE salary<(  SELECT MIN(salary)  FROM employees  WHERE job_id='IT_PROG') AND job_id <> 'IT_PROG';

#3.行子查询(结果集一行多列或者多行多列)#案例1.查询员工编号最小并且工资最高的员工信息.引入SELECT MIN(employee_id)FROM employees;=================SELECT MAX(salary)FROM employees;

SELECT *FROM employeesWHERE employee_id = ( SELECT MIN(employee_id) FROM employees)AND salary = ( SELECT MAX(salary) FROM employees     );

这种查询结果使用虚拟字段,单行操作符必须一致可以使用。查出来与上面同样的效果。SELECT *FROM employeesWHERE (employee_id,salary)=( SELECT MIN(employee_id), MAX(salary) FROM employees       );
#二.SELECT子查询#仅仅支持标量子查询,结果是一行一列#案例1.查询每个部门的员工个数SELECT d.*,(SELECT COUNT(*) FROM employees)FROM departments d; 

添加条件SELECT d.*,(SELECT COUNT(*)FROM employees eWHERE e.department_id=d.department_id) AS 个数FROM departments d; 

#案例2.查询员工号=102的部门名。SELECT department_nameFROM departments;==============SELECT employee_idFROM employeesWHERE employee_id = 102;

SELECT employee_id,( SELECT department_name FROM departments d WHERE e.department_id=d.department_id)FROM employees eWHERE employee_id=102;

#三.FROM 后面注意:将子查询结果充当一张表,要求必须起别名#案例:查询每个部门的平均工资等级。SELECT ROUND(AVG(salary),2),department_idFROM employeesGROUP BY department_id;

SELECT e.平均工资,j.grade_levelFROM job_grades AS j,(SELECT ROUND(AVG(salary),2) AS 平均工资,department_id FROM employees GROUP BY department_id) AS eWHERE e.平均工资 BETWEEN j.lowest_sal AND j.highest_sal;
#1999语法,老师答案SELECT e.*,j.grade_levelFROM (  SELECT ROUND(AVG(salary),2) AS 平均工资,department_id  FROM employees  GROUP BY department_id) AS eINNER JOIN job_grades jON e.平均工资 BETWEEN j.lowest_sal AND j.highest_sal;

#四.EXISTS后面(相关子查询)语法:EXISTS(完整的查询语句)备注:完整的查询语句可以是一行一列,可以使一行多列注意:先走外查询,然后根据某个字段的值再去过滤EXISTS 判断(布尔类型)值存不存在,结果只有两种:1有,0没有#引入SELECT EXISTS(SELECT employee_id FROM employees);

查询工资3W的员工信息SELECT EXISTS(SELECT * FROM employees WHERE salary=30000);

#案例引入.查询员工名和部门名#查员工名与部门编号SELECT first_name,department_idFROM employeesWHERE department_id;

#查部门名SELECT department_nameFROM departments;

#查员工名与部门名SELECT e.first_name,d.department_nameFROM employees eINNER JOIN (  SELECT department_name,department_id FROM departments) AS dON e.department_id=d.department_id;

#案例1..查有员工的部门名SELECT department_nameFROM departments dWHERE EXISTS( SELECT * FROM employees e WHERE d.department_id=e.department_id    );

使用IN代替EXISTS,同样是上面的结果SELECT department_nameFROM departments dWHERE d.department_id IN( SELECT department_id FROM employees     );
#案例2.查询没有女朋友的男神信息#IN方法SELECT *FROM boys boWHERE bo.id NOT IN( SELECT boyfriend_id FROM beauty be);===============#EXISTS方法SELECT *FROM boys boWHERE NOT EXISTS( SELECT boyfriend_id FROM beauty be WHERE bo.id=be.boyfriend_id);

进阶9:联合查询UNION 联合 合并:将多条查询语句的结果合并成一个结果。语法: 查询语句1 UNION 查询语句2 UNION ...应用场景:要查询的结果来自于多个表,且多个表没有直接的连接关系,但查询信息一致时。网页搜索内容,内容从不同的表中检索联合起来返回给用户。特点:1.要求多条查询语句的查询列数是一致的。2.要求多条查询语句的查询的每一列的类型和顺序最好一致。3.使用UNION关键字默认去重,如果使用UNION ALL全部展示,包含重复项
#引入案例1.:查询部门编号>90或者邮箱包含A的员工信息SELECT * FROM employeesWHERE email LIKE '%a%' OR department_id>90;联合查询SELECT * FROM employees WHERE email LIKE '%a%'UNIONSELECT * FROM employees WHERE department_id>90;

           感谢能认真读到这里的伙伴们,MySql查询部分结束,相信屏幕前的你照着我博客里的模板可以完成一些简单的SQL查询语句,SQL既然学了,以后还是要多练习一下,SQL1992与1999语法在主流的关系型数据库都是通用的。后续我会继续进行对MySql的知识进行扩展,感兴趣的同志互相关注一呗!o(^▽^)o

MySql的回顾六:子查询(内查询)Zozo慧聪商务网美森在中东做跨境生意,要了解什么?Listing优化的五大重要因素(精品分析)亚马逊美国站家用拖把桶类目市场调查数据报告乘高铁畅游华北 饱览北国迷人的冬日风景(图)_北京市旅游危楼高百尺 看芝加哥顶级摩天大楼西藏边境生态游 仓央嘉措情歌中的秘境

Comments

Popular posts from this blog

指纹浏览器定制开发全面助力企业安全与智能升级

跨境电商资讯:一文带你走进亚马逊19大海

利用 Google 购物广告促进销量的初学者指南