zoukankan      html  css  js  c++  java
  • MySQL 子查询

    子查询(嵌套查询)

    1. 理解
       一个查询语句中内嵌另一个完整的查询语句,被嵌套的语句 称为子查询 或者内查询
       外面的语句称为主查询 或者外查询
    2. 语法
       select (子查询)
       from (子查询)
       where (子查询)
    
    3. 特点
       1. 子查询 必须写在小括号中
       2. 子查询 优先执行  ,主查询需要用到子查询的结果
       3. 子查询结果
    	单行子查询:结果只有一个 搭配的符号 =  > < >= <= <>
    	多行子查询:结果多个     搭配的符号 in / not in / any(任一) / all (所有)
    

    #案例1:查询薪资比'De Haan'的薪资高的员工信息
    #step1: De Haan 的薪资
    	select * from employees where last_name = 'De Haan';
    #step2: 找到薪资比DeHaan薪资还要高的员工信息
    	#select * from employees where salary > 17000;
    	select * from employees where salary > 
          (select salary from employees where last_name = 'De Haan');
    
    
    #案例2:查询location_id 是1400 或者 1700的部门中所有的员工的信息
    
    select * from employees where department_id in 
    (select department_id from departments where location_id = 1400 or location_id = 1700);
    
    
    #案例3:查询其他工种中比工种为'IT_PROG'中的 所有员工薪资都低的员工薪资、名字
    select * from employees where salary <
    	(select min(salary) from employees where job_id = 'it_prog')
    
    select * from employees where salary <
    	(select salary from employees where job_id = 'it_prog' order by salary limit 1 )
    
    select * from employees where salary < all
    	(select salary from employees where job_id = 'it_prog')
    		
    
    
    查询的规则:
    	1. 单表能实现 不用多表
     	2. 多表能实现 不用子查询
     	
    	
    子查询效率比较低。 	
    
  • 相关阅读:
    Fiddler——基本常识
    Fiddler——抓包工具的使用
    Angular——单页面实例
    Angular——路由参数
    Angular——单页面与路由的使用
    Angular——配置模块与运行模块
    Angular——自定义服务
    Angular——$http
    Angular——内置服务
    Angular——依赖注入
  • 原文地址:https://www.cnblogs.com/conglingkaishi/p/15215410.html
Copyright © 2011-2022 走看看