码迷,mamicode.com
首页 > 数据库 > 详细

MySQL语法------11-----子查询

时间:2020-02-20 00:00:42      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:部门   max   oca   ati   employees   作用   编号   操作符   不同   


#进阶7:子查询
    含义:出现在其他语句中的select语句,称为子查询或者内查询
     外部的查询语句,成为主查询或者外查询

分类:
按照子查询出现的位置:
    select后面:
      仅仅支持标量子查询
    from后面:
      支持表子查询
    where或者having后面;
      标量子查询
      列子查询
      行子查询
    EXISTS后面(相关子查询)
      表子查询


按照结果集的行列数不同:
    标量子查询(结果集中只有一行一列)
    列子查询(结果集中只有一列多行)
    行子查询(结果集中有一行多列)
    表子查询(结果集中一般为多行多列)

#一、where或者having后面
    1. 标量子查询(单行子查询)
    2. 列子查询(多行子查询)
    3. 行子查询(多行子查询)
特点:
  小1:子查询放在小括号内
  小2;子查询一般放在条件的右侧
  小3:标量子查询。一般搭配着单行操作符使用
      > < >= <= = <>
列子查询,一般搭配着多行操作符使用
      in any/some all

 


#案例1:查询谁的工资比Abel的高
  select * from employees where salary >(
    select salary from employees where last_name =‘Abel‘
  );

#案例2:查询公司工资最少的员工的last_name job_id 和salary
  select last_name ,job_id ,salary from employees where salary=(
    select min(salary) from employees
  );


#案例4:查询最低工资大于50号部门的最低工资的部门id 和其最低工资
步骤1:先去查询50号部门员工的最低工资
select min(salary) from employees where department_id=‘50‘
步骤3:查询每个部门的最低工资
select department_id,min(salary) from employees GROUP BY department_id;
步骤3:查询最低工资大于50号部门的最低工资的部门id 和其最低工资
select department_id,min(salary) from employees where 1=1 GROUP BY department_id
HAVING min(salary)>(select min(salary) from employees where department_id=‘50‘)

2.列子查询(多行子查询)
案例1:返回location_id是1400或者1700的部门中的所有员工的姓名
desc employees; select count(1) from employees
select last_name from employees where department_id in(
  SELECT DISTINCT department_id from locations where location_id in(‘1400‘,‘1700‘)
);
或者
select last_name from employees where department_id =any(
  SELECT DISTINCT department_id from locations where location_id in(‘1400‘,‘1700‘)
);

3.案例3:查询工种比IT_PROG任意工资少的其他工种的员工姓名,工资,工号,工种编码
select last_name,salary,employee_id,job_id from employees where salary <any(
  select DISTINCT salary from employees where job_id=‘IT_PROG‘
) and job_id<>‘IT_PROG‘;--76 注意:any和some的作用是一样的

或者:
select last_name,salary,employee_id,job_id from employees where salary <(
  select max(salary) from employees where job_id=‘IT_PROG‘
) and job_id<>‘IT_PROG‘;--76

3.案例4:查询工种比IT_PROG工种所有员工的工资都低的其他工种的员工姓名,工资,工号,工种编码
select last_name,salary,employee_id,job_id from employees where salary <all(
  select DISTINCT salary from employees where job_id=‘IT_PROG‘
) and job_id<>‘IT_PROG‘;--44

或者:
select last_name,salary,employee_id,job_id from employees where salary <(
  select min(salary) from employees where job_id=‘IT_PROG‘
) and job_id<>‘IT_PROG‘;--44

3.行子查询
案例:查询员工编号最小并且工资最高的员工信息
步骤1;查询最小员工编号
select min(employee_id) FROM employees;
步骤2:查询最高工资
select max(salary) from employees;
步骤3:总体查询
select * from employees where employee_id=(select min(employee_id) FROM employees) and salary=(select max(salary) from employees)
换一种神奇的写法,行子查询
select * from employees where (employee_id,salary)=(select min(employee_id),max(salary) FROM employees)--同样能够查询出来结果

MySQL语法------11-----子查询

标签:部门   max   oca   ati   employees   作用   编号   操作符   不同   

原文地址:https://www.cnblogs.com/dongyaotou/p/12333857.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!