1. sql规范

1.不区分大小写,但建议关键字大写,表名、列名小写;
2.每条命令最好用分号结尾;
3.每条命令根据需要,可以进行缩进或换行;
4.注释
#

/注释内容/

2.基础查询

2.1语法

select 查询列表 from 表名;
特点:
1)查询列表可以是:表中的字段,常量值,表达式,函数
2)查询结果是一个虚拟的表格

2.2 示例

1)查询表中的单个字段

  1. mysql> select last_name from employees;
  2. +-------------+
  3. | last_name |
  4. +-------------+
  5. | K_ing |
  6. | Kochhar |
  7. | De Haan |
  8. | Hunold |
  9. | Ernst |
  10. | Austin |
  11. | Pataballa |
  12. +-------------+

2)查询表中的多个字段

  1. mysql> select last_name,salary from employees;
  2. +-------------+----------+
  3. | last_name | salary |
  4. +-------------+----------+
  5. | K_ing | 24000.00 |
  6. | Kochhar | 17000.00 |
  7. | De Haan | 17000.00 |
  8. | Hunold | 9000.00 |
  9. | Ernst | 6000.00 |
  10. | Austin | 4800.00 |
  11. +-------------+----------+

3)查看表中的所有字段

  1. select * from employees;

4)查询常量值

  1. mysql> select 100;
  2. +-----+
  3. | 100 |
  4. +-----+
  5. | 100 |
  6. +-----+
  7. 1 row in set (0.00 sec)
  8. mysql> select 'chenwendong';
  9. +-------------+
  10. | chenwendong |
  11. +-------------+
  12. | chenwendong |
  13. +-------------+
  14. 1 row in set (0.00 sec)

5)查询表达式

  1. mysql> select 100%98;
  2. +--------+
  3. | 100%98 |
  4. +--------+
  5. | 2 |
  6. +--------+
  7. 1 row in set (0.00 sec)

6)查询函数

  1. mysql> select version();
  2. +-----------+
  3. | version() |
  4. +-----------+
  5. | 5.7.33 |
  6. +-----------+
  7. 1 row in set (0.00 sec)

7)起别名

  1. 方式1:使用as
  2. mysql> select 100%98 as 结果;
  3. +--------+
  4. | 结果 |
  5. +--------+
  6. | 2 |
  7. +--------+
  8. 1 row in set (0.00 sec)
  9. mysql> select last_name as 姓,first_name as from employees;
  10. +-------------+-------------+
  11. | | |
  12. +-------------+-------------+
  13. | K_ing | Steven |
  14. | Kochhar | Neena |
  15. | De Haan | Lex |
  16. | Hunold | Alexander |
  17. | Ernst | Bruce |
  18. | Austin | David |
  19. +-------------+-------------+
  20. 方式2:使用空格
  21. mysql> select 100%98 结果;
  22. +--------+
  23. | 结果 |
  24. +--------+
  25. | 2 |
  26. +--------+
  27. 1 row in set (0.00 sec)
  28. mysql> select last_name 姓,first_name from employees;
  29. +-------------+-------------+
  30. | | |
  31. +-------------+-------------+
  32. | K_ing | Steven |
  33. | Kochhar | Neena |
  34. | De Haan | Lex |
  35. | Hunold | Alexander |
  36. | Ernst | Bruce |
  37. +-------------+-------------+
  38. 案例:查询salary,显示结果为out put
  39. mysql> select salary "out put" from employees;
  40. +----------+
  41. | out put |
  42. +----------+
  43. | 24000.00 |
  44. | 17000.00 |
  45. | 17000.00 |
  46. | 9000.00 |
  47. | 6000.00 |
  48. | 4800.00 |
  49. | 4800.00 |
  50. | 4200.00 |
  51. | 12000.00 |
  52. | 9000.00 |
  53. +----------+
  54. 10 rows in set (0.00 sec)

8)去重
案例:查询员工表中涉及到的所有部门编号

  1. mysql> select distinct department_id from employees;
  2. +---------------+
  3. | department_id |
  4. +---------------+
  5. | NULL |
  6. | 10 |
  7. | 20 |
  8. | 30 |
  9. | 40 |
  10. | 50 |
  11. | 60 |
  12. | 70 |
  13. | 80 |
  14. | 90 |
  15. | 100 |
  16. | 110 |
  17. +---------------+
  18. 12 rows in set (0.01 sec)

9)’+’号的作用
mysql中的+号仅仅能做为运算符

  1. 两个都是数值型,则做加法运算
  2. mysql> select 100+90;
  3. +--------+
  4. | 100+90 |
  5. +--------+
  6. | 190 |
  7. +--------+
  8. 1 row in set (0.00 sec)
  9. 其中一方为字符型,试图将字符型转换成数值型,如果转换成功,继续做加法运算,转换失败,字符型当0处理
  10. mysql> select '100'+90;
  11. +----------+
  12. | '100'+90 |
  13. +----------+
  14. | 190 |
  15. +----------+
  16. 1 row in set (0.00 sec)
  17. mysql> select 'cwd' + 90;
  18. +------------+
  19. | 'cwd' + 90 |
  20. +------------+
  21. | 90 |
  22. +------------+
  23. 1 row in set, 1 warning (0.00 sec)
  24. 只要其中一方为null,运算后均为null
  25. mysql> select null + 90 ;
  26. +-----------+
  27. | null + 90 |
  28. +-----------+
  29. | NULL |
  30. +-----------+
  31. 1 row in set (0.00 sec)
  32. mysql> select 'cwd'+null;
  33. +------------+
  34. | 'cwd'+null |
  35. +------------+
  36. | NULL |
  37. +------------+
  38. 1 row in set, 1 warning (0.00 sec)
  39. 可以对列进行判断,如果为null,当0处理;
  40. mysql> select concat(last_name,ifnull(commission_pct,0)) from employees limit 10 ;
  41. +--------------------------------------------+
  42. | concat(last_name,ifnull(commission_pct,0)) |
  43. +--------------------------------------------+
  44. | K_ing0.00 |
  45. | Kochhar0.00 |
  46. | De Haan0.00 |
  47. | Hunold0.00 |
  48. | Ernst0.00 |
  49. | Austin0.00 |
  50. | Pataballa0.00 |
  51. | Lorentz0.00 |
  52. | Greenberg0.00 |
  53. | Faviet0.00 |
  54. +--------------------------------------------+
  55. 10 rows in set (0.00 sec)

案例:查询员工名和姓连接成一个字段,并显示为姓名

  1. mysql> select concat(last_name,first_name) as 姓名 from employees;
  2. +-----------------+
  3. | 姓名 |
  4. +-----------------+
  5. | K_ingSteven |
  6. | KochharNeena |
  7. | De HaanLex |
  8. | HunoldAlexander |
  9. | ErnstBruce |
  10. | AustinDavid |
  11. | PataballaValli |
  12. | LorentzDiana |
  13. | GreenbergNancy |
  14. | FavietDaniel |
  15. +-----------------+
  16. 10 rows in set (0.00 sec)

3.条件查询

3.1语法

select 查询列表 from 表名 where 筛选条件;
分类
1.按条件表达式筛选
条件运算符:> < = != <> >= <=

2.按逻辑表达式筛选
逻辑运算符: && || ! 或者 and or not
作用:用于连接条件表达式

3.模糊查询
like
between and
in
is null

3.2示例

1)查询员工工资>12K员工的信息;

  1. mysql> select employee_id,first_name,last_name,salary from employees where salary > 12000 ;
  2. +-------------+------------+-----------+----------+
  3. | employee_id | first_name | last_name | salary |
  4. +-------------+------------+-----------+----------+
  5. | 100 | Steven | K_ing | 24000.00 |
  6. | 101 | Neena | Kochhar | 17000.00 |
  7. | 102 | Lex | De Haan | 17000.00 |
  8. | 145 | John | Russell | 14000.00 |
  9. | 146 | Karen | Partners | 13500.00 |
  10. | 201 | Michael | Hartstein | 13000.00 |
  11. +-------------+------------+-----------+----------+
  12. 6 rows in set (0.00 sec)

2)查询部门编号不等于90号的员工名和部门编号

  1. mysql> select last_name, department_id from employees where department_id <>90;
  2. +-------------+---------------+
  3. | last_name | department_id |
  4. +-------------+---------------+
  5. | Hunold | 60 |
  6. | Ernst | 60 |
  7. | Austin | 60 |
  8. +-------------+---------------+

3)查询工资在10-20K之间的员工名、工资、以及奖金

  1. mysql> select last_name,salary,ifnull(commission_pct,0) from employees where salary between 10000 and 20000;
  2. +-----------+----------+--------------------------+
  3. | last_name | salary | ifnull(commission_pct,0) |
  4. +-----------+----------+--------------------------+
  5. | Kochhar | 17000.00 | 0.00 |
  6. | De Haan | 17000.00 | 0.00 |
  7. | Greenberg | 12000.00 | 0.00 |
  8. | Raphaely | 11000.00 | 0.00 |
  9. | Russell | 14000.00 | 0.40 |
  10. | Partners | 13500.00 | 0.30 |
  11. | Errazuriz | 12000.00 | 0.30 |
  12. | Cambrault | 11000.00 | 0.30 |
  13. | Zlotkey | 10500.00 | 0.20 |
  14. | Tucker | 10000.00 | 0.30 |
  15. | K_ing | 10000.00 | 0.35 |
  16. | Vishney | 10500.00 | 0.25 |
  17. | Ozer | 11500.00 | 0.25 |
  18. | Bloom | 10000.00 | 0.20 |
  19. | Abel | 11000.00 | 0.30 |
  20. | Hartstein | 13000.00 | 0.00 |
  21. | Baer | 10000.00 | 0.00 |
  22. | Higgins | 12000.00 | 0.00 |
  23. +-----------+----------+--------------------------+

4)查询部门编号不是在90-100之间,或者工资高于15K的员工信息

  1. mysql> select last_name,salary,department_id from employees where department_id <90 or department_id >110 or salary >15000 limit 10 ;
  2. +-----------+----------+---------------+
  3. | last_name | salary | department_id |
  4. +-----------+----------+---------------+
  5. | K_ing | 24000.00 | 90 |
  6. | Kochhar | 17000.00 | 90 |
  7. | De Haan | 17000.00 | 90 |
  8. | Hunold | 9000.00 | 60 |
  9. | Ernst | 6000.00 | 60 |
  10. | Austin | 4800.00 | 60 |
  11. | Pataballa | 4800.00 | 60 |
  12. | Lorentz | 4200.00 | 60 |
  13. | Raphaely | 11000.00 | 30 |
  14. | Khoo | 3100.00 | 30 |
  15. +-----------+----------+---------------+
  16. 10 rows in set (0.00 sec)
  17. mysql> select last_name,salary,department_id from employees where not (department_id >=90 and department_id <=110) or salary >15000 limit 10;
  18. +-----------+----------+---------------+
  19. | last_name | salary | department_id |
  20. +-----------+----------+---------------+
  21. | K_ing | 24000.00 | 90 |
  22. | Kochhar | 17000.00 | 90 |
  23. | De Haan | 17000.00 | 90 |
  24. | Hunold | 9000.00 | 60 |
  25. | Ernst | 6000.00 | 60 |
  26. | Austin | 4800.00 | 60 |
  27. | Pataballa | 4800.00 | 60 |
  28. | Lorentz | 4200.00 | 60 |
  29. | Raphaely | 11000.00 | 30 |
  30. | Khoo | 3100.00 | 30 |
  31. +-----------+----------+---------------+
  32. 10 rows in set (0.00 sec)

5)查询员工名中包含字符a的员工信息

  1. mysql> select last_name from employees where last_name like '%a%' limit 10 ;
  2. +-----------+
  3. | last_name |
  4. +-----------+
  5. | Kochhar |
  6. | De Haan |
  7. | Austin |
  8. | Pataballa |
  9. | Faviet |
  10. | Sciarra |
  11. | Urman |
  12. | Raphaely |
  13. | Baida |
  14. | Tobias |
  15. +-----------+
  16. 10 rows in set (0.00 sec)

6)查询员工名中第三个字符为n,第五个字符为l的员工名和工资

  1. mysql> select last_name, salary from employees where last_name like '__n_l%';
  2. +-----------+---------+
  3. | last_name | salary |
  4. +-----------+---------+
  5. | Hunold | 9000.00 |
  6. +-----------+---------+
  7. 1 row in set (0.00 sec)

7)查询员工名中第二个字符为_的员工名

  1. mysql> select last_name from employees where last_name like '_\_%';
  2. +-----------+
  3. | last_name |
  4. +-----------+
  5. | K_ing |
  6. | K_ing |
  7. +-----------+
  8. 2 rows in set (0.00 sec)

8)查询员工编号在100-120之间的员工名和工资

  1. mysql> select employee_id ,last_name,salary from employees where employee_id between 100 and 120 limit 10;
  2. +-------------+-----------+----------+
  3. | employee_id | last_name | salary |
  4. +-------------+-----------+----------+
  5. | 100 | K_ing | 24000.00 |
  6. | 101 | Kochhar | 17000.00 |
  7. | 102 | De Haan | 17000.00 |
  8. | 103 | Hunold | 9000.00 |
  9. | 104 | Ernst | 6000.00 |
  10. | 105 | Austin | 4800.00 |
  11. | 106 | Pataballa | 4800.00 |
  12. | 107 | Lorentz | 4200.00 |
  13. | 108 | Greenberg | 12000.00 |
  14. | 109 | Faviet | 9000.00 |
  15. +-------------+-----------+----------+
  16. 10 rows in set (0.00 sec)

9)查询员工的工种编号是IT_PROG、AD_VP、AD_PRES中的一个员工名和工种编号

  1. mysql> select last_name,job_id from employees where job_id in ('IT_PROT','AD_VP','AD_PRES');
  2. +-----------+---------+
  3. | last_name | job_id |
  4. +-----------+---------+
  5. | K_ing | AD_PRES |
  6. | Kochhar | AD_VP |
  7. | De Haan | AD_VP |
  8. +-----------+---------+
  9. 3 rows in set (0.00 sec)

10)查询没有奖金的员工名和奖金率

  1. mysql> select last_name,commission_pct from employees where commission_pct is null;
  2. +-------------+----------------+
  3. | last_name | commission_pct |
  4. +-------------+----------------+
  5. | K_ing | NULL |
  6. | Kochhar | NULL |
  7. | De Haan | NULL |
  8. | Hunold | NULL |
  9. | Ernst | NULL |
  10. | Austin | NULL |
  11. | Pataballa | NULL |
  12. | Lorentz | NULL |
  13. +-------------+----------------+
  14. mysql> select last_name,commission_pct from employees where commission_pct <=> null;
  15. +-------------+----------------+
  16. | last_name | commission_pct |
  17. +-------------+----------------+
  18. | K_ing | NULL |
  19. | Kochhar | NULL |
  20. | De Haan | NULL |
  21. | Hunold | NULL |
  22. | Ernst | NULL |
  23. | Austin | NULL |
  24. | Pataballa | NULL |
  25. | Lorentz | NULL |
  26. +-------------+----------------+
  27. is null 只能判断null
  28. <=> 既可以判断null值,又可以判断普通的数值

3.3 基础查询练习

  1. 查询工资大于 12000 的员工姓名和工资 ```sql mysql> select salary,last_name from employees where salary > 12000; +—————+—————-+ | salary | last_name | +—————+—————-+ | 24000.00 | K_ing | | 17000.00 | Kochhar | | 17000.00 | De Haan | | 14000.00 | Russell | | 13500.00 | Partners | | 13000.00 | Hartstein | +—————+—————-+ 6 rows in set (0.00 sec)
  1. 2. 查询员工号为 176 的员工的姓名和部门号和年薪
  2. ```sql
  3. mysql> select employee_id,last_name,department_id,salary*12*(1+ifnull(commission_pct,0))
  4. from employees where empployee_id =176
  5. +-------------+-----------+---------------+----------------------------------------+
  6. | employee_id | last_name | department_id | salary*12*(1+ifnull(commission_pct,0)) |
  7. +-------------+-----------+---------------+----------------------------------------+
  8. | 176 | Taylor | 80 | 123840.00 |
  9. +-------------+-----------+---------------+----------------------------------------+
  10. 1 row in set (0.00 sec)
  1. 选择工资不在 5000 到 12000 的员工的姓名和工资 ```sql mysql> select last_name,salary from employees
    where salary not between 5000 and 12000 limit 10; +—————-+—————+ | last_name | salary | +—————-+—————+ | K_ing | 24000.00 | | Kochhar | 17000.00 | | De Haan | 17000.00 | | Austin | 4800.00 | | Pataballa | 4800.00 | | Lorentz | 4200.00 | | Khoo | 3100.00 | | Baida | 2900.00 | | Tobias | 2800.00 | | Himuro | 2600.00 | +—————-+—————+ 10 rows in set (0.00 sec)
  1. 4. 选择在 20 50 号部门工作的员工姓名和部门号
  2. ```sql
  3. mysql> select last_name,department_id from employees
  4. where department_id in(20,50) limit 10;
  5. +-------------+---------------+
  6. | last_name | department_id |
  7. +-------------+---------------+
  8. | Hartstein | 20 |
  9. | Fay | 20 |
  10. | Weiss | 50 |
  11. | Fripp | 50 |
  12. | Kaufling | 50 |
  13. | Vollman | 50 |
  14. | Mourgos | 50 |
  15. | Nayer | 50 |
  16. | Mikkilineni | 50 |
  17. | Landry | 50 |
  18. +-------------+---------------+
  19. 10 rows in set (0.00 sec)
  1. 选择公司中没有管理者的员工姓名及 job_id ```sql mysql> select last_name,job_id from employees where manager_id <=> null; +—————-+————-+ | last_name | job_id | +—————-+————-+ | K_ing | AD_PRES | +—————-+————-+ 1 row in set (0.00 sec)
  1. 6. 选择公司中有奖金的员工姓名,工资
  2. ```sql
  3. mysql> select last_name, salary,commission_pct from employees
  4. where commission_pct is not null limit 10;
  5. +-----------+----------+----------------+
  6. | last_name | salary | commission_pct |
  7. +-----------+----------+----------------+
  8. | Russell | 14000.00 | 0.40 |
  9. | Partners | 13500.00 | 0.30 |
  10. | Errazuriz | 12000.00 | 0.30 |
  11. | Cambrault | 11000.00 | 0.30 |
  12. | Zlotkey | 10500.00 | 0.20 |
  13. | Tucker | 10000.00 | 0.30 |
  14. | Bernstein | 9500.00 | 0.25 |
  15. | Hall | 9000.00 | 0.25 |
  16. | Olsen | 8000.00 | 0.20 |
  17. | Cambrault | 7500.00 | 0.20 |
  18. +-----------+----------+----------------+
  19. 10 rows in set (0.00 sec)
  1. 选择员工姓名的第三个字母是 a 的员工姓名 ```sql mysql> select last_name from employees where last_name like ‘__a%’; +—————-+ | last_name | +—————-+ | Grant | | Grant | | Whalen | +—————-+ 3 rows in set (0.00 sec)
  1. 8. 选择姓名中有字母 a e 的员工姓名
  2. ```sql
  3. mysql> select last_name from employees
  4. where last_name like '%a%' and last_name like '%e%' limit 10;
  5. +------------+
  6. | last_name |
  7. +------------+
  8. | De Haan |
  9. | Faviet |
  10. | Raphaely |
  11. | Colmenares |
  12. | Nayer |
  13. | Markle |
  14. | Philtanker |
  15. | Patel |
  16. | Davies |
  17. | Partners |
  18. +------------+
  19. 10 rows in set (0.00 sec)
  1. 显示出表 employees 表中 first_name 以 ‘e’结尾的员工信息 ```sql mysql> select first_name from employees where first_name like ‘%e’; +——————+ | first_name | +——————+ | Bruce | | Irene | | Mozhe | | Renske | | Nanette | | Janette | | Louise | | Danielle | | Vance | +——————+ 9 rows in set (0.00 sec)
  1. 10. 显示出表 employees 部门编号在 80-100 之间 的姓名、职位
  2. ```sql
  3. mysql> select last_name ,job_id from employees
  4. where department_id between 80 and 100 limit 10 ;
  5. +-----------+--------+
  6. | last_name | job_id |
  7. +-----------+--------+
  8. | Russell | SA_MAN |
  9. | Partners | SA_MAN |
  10. | Errazuriz | SA_MAN |
  11. | Cambrault | SA_MAN |
  12. | Zlotkey | SA_MAN |
  13. | Tucker | SA_REP |
  14. | Bernstein | SA_REP |
  15. | Hall | SA_REP |
  16. | Olsen | SA_REP |
  17. | Cambrault | SA_REP |
  18. +-----------+--------+
  19. 10 rows in set (0.00 sec)
  1. 显示出表 employees 的 manager_id 是 100,101,110 的员工姓名、职位 ```sql mysql> select last_name,job_id from employees where manager_id in (100,101,110) limit 10 ; +—————-+————+ | last_name | job_id | +—————-+————+ | Kochhar | AD_VP | | De Haan | AD_VP | | Greenberg | FI_MGR | | Raphaely | PU_MAN | | Weiss | ST_MAN | | Fripp | ST_MAN | | Kaufling | ST_MAN | | Vollman | ST_MAN | | Mourgos | ST_MAN | | Russell | SA_MAN | +—————-+————+ 10 rows in set (0.00 sec)
  1. <a name="qxmoy"></a>
  2. ## 4.排序查询
  3. <a name="YNINC"></a>
  4. ### 4.1 语法
  5. select 查询列表 from 表 order by 排序列表 asc | desc (升序|降序),默认是升序<br />order by子句可以支持单个字段、多个字段、表达式、函数、别名<br />order by子句一般放在查询语句的最后面,limit字句除外
  6. <a name="VD4N0"></a>
  7. ### 4.2 案例
  8. 1)查询员工信息,要求工资从高到低排序
  9. ```sql
  10. mysql> select last_name,salary from employees
  11. order by salary desc limit 10;
  12. +-----------+----------+
  13. | last_name | salary |
  14. +-----------+----------+
  15. | K_ing | 24000.00 |
  16. | Kochhar | 17000.00 |
  17. | De Haan | 17000.00 |
  18. | Russell | 14000.00 |
  19. | Partners | 13500.00 |
  20. | Hartstein | 13000.00 |
  21. | Higgins | 12000.00 |
  22. | Greenberg | 12000.00 |
  23. | Errazuriz | 12000.00 |
  24. | Ozer | 11500.00 |
  25. +-----------+----------+
  26. 10 rows in set (0.00 sec)

2)查询部门编号》=90的员工信息,按入职的先后顺序进行排序

  1. mysql> select last_name ,hiredate from employees
  2. where department_id >=90 order by hiredate;
  3. +-----------+---------------------+
  4. | last_name | hiredate |
  5. +-----------+---------------------+
  6. | K_ing | 1992-04-03 00:00:00 |
  7. | Kochhar | 1992-04-03 00:00:00 |
  8. | De Haan | 1992-04-03 00:00:00 |
  9. | Greenberg | 1998-03-03 00:00:00 |
  10. | Faviet | 1998-03-03 00:00:00 |
  11. | Chen | 2000-09-09 00:00:00 |
  12. | Sciarra | 2000-09-09 00:00:00 |
  13. | Urman | 2000-09-09 00:00:00 |
  14. | Popp | 2000-09-09 00:00:00 |
  15. | Higgins | 2016-03-03 00:00:00 |
  16. | Gietz | 2016-03-03 00:00:00 |
  17. +-----------+---------------------+
  18. 11 rows in set (0.00 sec)

3)按年薪的高低显示员工的信息和年薪

  1. mysql> select last_name, salary*12*(1+ifnull(commission_pct,0)) as 年薪 from employees
  2. order by 年薪 desc limit 10;
  3. +-----------+-----------+
  4. | last_name | 年薪 |
  5. +-----------+-----------+
  6. | K_ing | 288000.00 |
  7. | Russell | 235200.00 |
  8. | Partners | 210600.00 |
  9. | Kochhar | 204000.00 |
  10. | De Haan | 204000.00 |
  11. | Errazuriz | 187200.00 |
  12. | Ozer | 172500.00 |
  13. | Cambrault | 171600.00 |
  14. | Abel | 171600.00 |
  15. | K_ing | 162000.00 |
  16. +-----------+-----------+
  17. 10 rows in set (0.00 sec)

4)按姓名的长度显示员工的姓名和工资

  1. mysql> select last_name, salary*12*(1+ifnull(commission_pct,0)) from employees
  2. order by length(last_name) desc limit 10;
  3. +-------------+----------------------------------------+
  4. | last_name | salary*12*(1+ifnull(commission_pct,0)) |
  5. +-------------+----------------------------------------+
  6. | Mikkilineni | 32400.00 |
  7. | Colmenares | 30000.00 |
  8. | Philtanker | 26400.00 |
  9. | Livingston | 120960.00 |
  10. | Cambrault | 171600.00 |
  11. | Bernstein | 142500.00 |
  12. | Pataballa | 57600.00 |
  13. | Cambrault | 108000.00 |
  14. | Greenberg | 144000.00 |
  15. | Errazuriz | 187200.00 |
  16. +-------------+----------------------------------------+
  17. 10 rows in set (0.00 sec)

5)查询员工信息,要求先按工资排序,再按员工编号排序

  1. mysql> select employee_id,last_name,salary from employees
  2. order by salary asc, employee_id desc limit 10;
  3. +-------------+------------+---------+
  4. | employee_id | last_name | salary |
  5. +-------------+------------+---------+
  6. | 132 | Olson | 2100.00 |
  7. | 136 | Philtanker | 2200.00 |
  8. | 128 | Markle | 2200.00 |
  9. | 135 | Gee | 2400.00 |
  10. | 127 | Landry | 2400.00 |
  11. | 191 | Perkins | 2500.00 |
  12. | 182 | Sullivan | 2500.00 |
  13. | 144 | Vargas | 2500.00 |
  14. | 140 | Patel | 2500.00 |
  15. | 131 | Marlow | 2500.00 |
  16. +-------------+------------+---------+
  17. 10 rows in set (0.01 sec)

5.常见函数

5.1 简介

类似于Java的方法,将一组逻辑语句封装在方法中,对外暴露方法名;
好处:
1.隐藏了实现细节
2.提高了代码的重用性

调用:slect 函数名(实参列表) 【from 表】

分类:
1.单行函数:concat、length、ifnull等
2.分组函数,做统计使用,又称为统计函数

5.1 案例

1)字符函数
1.1)length 获取参数值得字节个数;

  1. mysql> select length('aaa');
  2. +---------------+
  3. | length('aaa') |
  4. +---------------+
  5. | 3 |
  6. +---------------+
  7. 1 row in set (0.00 sec)
  8. mysql> select length('陈文东');
  9. +---------------------+
  10. | length('陈文东') |
  11. +---------------------+
  12. | 9 |
  13. +---------------------+
  14. 1 row in set (0.00 sec)

1.2)concat 拼接字符串

  1. mysql> select concat(last_name,'_',first_name) as 姓名 from employees limit 10;
  2. +------------------+
  3. | 姓名 |
  4. +------------------+
  5. | K_ing_Steven |
  6. | Kochhar_Neena |
  7. | De Haan_Lex |
  8. | Hunold_Alexander |
  9. | Ernst_Bruce |
  10. | Austin_David |
  11. | Pataballa_Valli |
  12. | Lorentz_Diana |
  13. | Greenberg_Nancy |
  14. | Faviet_Daniel |
  15. +------------------+
  16. 10 rows in set (0.00 sec)

1.3)upper、lower

  1. mysql> select upper('cwd');
  2. +--------------+
  3. | upper('cwd') |
  4. +--------------+
  5. | CWD |
  6. +--------------+
  7. 1 row in set (0.00 sec)
  8. mysql> select lower('CWD');
  9. +--------------+
  10. | lower('CWD') |
  11. +--------------+
  12. | cwd |
  13. +--------------+
  14. 1 row in set (0.00 sec)

1.4)把姓变大写,名变小写,然后拼接

  1. mysql> select concat(upper(last_name),'_',lower(first_name)) from employees limit 10;
  2. +------------------------------------------------+
  3. | concat(upper(last_name),'_',lower(first_name)) |
  4. +------------------------------------------------+
  5. | K_ING_steven |
  6. | KOCHHAR_neena |
  7. | DE HAAN_lex |
  8. | HUNOLD_alexander |
  9. | ERNST_bruce |
  10. | AUSTIN_david |
  11. | PATABALLA_valli |
  12. | LORENTZ_diana |
  13. | GREENBERG_nancy |
  14. | FAVIET_daniel |
  15. +------------------------------------------------+
  16. 10 rows in set (0.00 sec)

1.5)substr、substring截取字符串
截取从指定索引处后面的所有字符

  1. mysql> select substr('大虎是一只猫',4);
  2. +--------------------------------+
  3. | substr('大虎是一只猫',4) |
  4. +--------------------------------+
  5. | 一只猫 |
  6. +--------------------------------+
  7. 1 row in set (0.00 sec)

截取从指定索引处指定字符长度的字符

  1. mysql> select substr('大虎是一只猫',4,2);
  2. +----------------------------------+
  3. | substr('大虎是一只猫',4,2) |
  4. +----------------------------------+
  5. | 一只 |
  6. +----------------------------------+
  7. 1 row in set (0.00 sec)

1.6)姓名中首字符大写,其他字符小写然后用_拼接,显示出来

  1. mysql> select concat(substr(last_name,1,1),'_',substr(last_name,2)) from employees limit 10;
  2. +-------------------------------------------------------+
  3. | concat(substr(last_name,1,1),'_',substr(last_name,2)) |
  4. +-------------------------------------------------------+
  5. | K__ing |
  6. | K_ochhar |
  7. | D_e Haan |
  8. | H_unold |
  9. | E_rnst |
  10. | A_ustin |
  11. | P_ataballa |
  12. | L_orentz |
  13. | G_reenberg |
  14. | F_aviet |
  15. +-------------------------------------------------------+
  16. 10 rows in set (0.00 sec)

1.7)instr返回子字符串在大字符串中第一次出现的索引,如果找不到返回0

  1. mysql> select instr('大虎是我家的猫','猫');
  2. +--------------------------------------+
  3. | instr('大虎是我家的猫','猫') |
  4. +--------------------------------------+
  5. | 7 |
  6. +--------------------------------------+
  7. 1 row in set (0.00 sec)
  8. mysql> select instr('大虎是我家的猫','狗');
  9. +--------------------------------------+
  10. | instr('大虎是我家的猫','狗') |
  11. +--------------------------------------+
  12. | 0 |
  13. +--------------------------------------+
  14. 1 row in set (0.00 sec)
  15. mysql>

1.8)trim,去除字符串中指定的内容

  1. mysql> select length(trim(' 大虎 '));
  2. +-------------------------------+
  3. | length(trim(' 大虎 ')) |
  4. +-------------------------------+
  5. | 6 |
  6. +-------------------------------+
  7. 1 row in set (0.00 sec)
  8. mysql> select (trim('a' from 'aaa大虎aaa'));
  9. +---------------------------------+
  10. | (trim('a' from 'aaa大虎aaa')) |
  11. +---------------------------------+
  12. | 大虎 |
  13. +---------------------------------+
  14. 1 row in set (0.00 sec)

1.9)lpad用指定的字符实现左填充指定长度,rpad一样。

  1. mysql> select lpad('大虎',10,'a');
  2. +-----------------------+
  3. | lpad('大虎',10,'a') |
  4. +-----------------------+
  5. | aaaaaaaa大虎 |
  6. +-----------------------+
  7. 1 row in set (0.00 sec)
  8. mysql>

1.20)replace 替换

  1. mysql> select replace('大虎是一只猫','猫','猪');
  2. +-------------------------------------------+
  3. | replace('大虎是一只猫','猫','猪') |
  4. +-------------------------------------------+
  5. | 大虎是一只猪 |
  6. +-------------------------------------------+
  7. 1 row in set (0.00 sec)

1.21)round 四舍五入

  1. mysql> select round(1.65);
  2. +-------------+
  3. | round(1.65) |
  4. +-------------+
  5. | 2 |
  6. +-------------+
  7. 1 row in set (0.00 sec)

1.22)ceil向上取整

  1. mysql> select ceil(1.02);
  2. +------------+
  3. | ceil(1.02) |
  4. +------------+
  5. | 2 |
  6. +------------+
  7. 1 row in set (0.00 sec)

1.23)向下取整

  1. mysql> select floor(1.02);
  2. +-------------+
  3. | floor(1.02) |
  4. +-------------+
  5. | 1 |
  6. +-------------+
  7. 1 row in set (0.00 sec)

1.24)截断,取小数点后几位

  1. mysql> select truncate(1.222222222,1);
  2. +-------------------------+
  3. | truncate(1.222222222,1) |
  4. +-------------------------+
  5. | 1.2 |
  6. +-------------------------+
  7. 1 row in set (0.00 sec)

1.25)取模

  1. mysql> select (10%3);
  2. +--------+
  3. | (10%3) |
  4. +--------+
  5. | 1 |
  6. +--------+
  7. 1 row in set (0.00 sec)
  8. mysql> select mod(10,3);
  9. +-----------+
  10. | mod(10,3) |
  11. +-----------+
  12. | 1 |
  13. +-----------+
  14. 1 row in set (0.00 sec)

1.26)now 返回当前系统日期+时间

  1. mysql> select now();
  2. +---------------------+
  3. | now() |
  4. +---------------------+
  5. | 2021-06-09 05:08:12 |
  6. +---------------------+
  7. 1 row in set (0.00 sec)
  8. mysql> select curdate();
  9. +------------+
  10. | curdate() |
  11. +------------+
  12. | 2021-06-09 |
  13. +------------+
  14. 1 row in set (0.00 sec)
  15. mysql> select curtime();
  16. +-----------+
  17. | curtime() |
  18. +-----------+
  19. | 21:10:09 |
  20. +-----------+
  21. 1 row in set (0.00 sec)
  22. 获取年
  23. mysql> select YEAR(curdate());
  24. +-----------------+
  25. | YEAR(curdate()) |
  26. +-----------------+
  27. | 2021 |
  28. +-----------------+
  29. 1 row in set (0.00 sec)
  30. mysql>
  31. mysql> select last_name,YEAR(hiredate) from employees limit 10 ;
  32. +-----------+------+
  33. | last_name | |
  34. +-----------+------+
  35. | K_ing | 1992 |
  36. | Kochhar | 1992 |
  37. | De Haan | 1992 |
  38. | Hunold | 1992 |
  39. | Ernst | 1992 |
  40. | Austin | 1998 |
  41. | Pataballa | 1998 |
  42. | Lorentz | 1998 |
  43. | Greenberg | 1998 |
  44. | Faviet | 1998 |
  45. +-----------+------+
  46. 10 rows in set (0.00 sec)
  47. mysql> select last_name, hiredate from employees where hiredate ='1992-4-3';
  48. +-----------+---------------------+
  49. | last_name | hiredate |
  50. +-----------+---------------------+
  51. | K_ing | 1992-04-03 00:00:00 |
  52. | Kochhar | 1992-04-03 00:00:00 |
  53. | De Haan | 1992-04-03 00:00:00 |
  54. | Hunold | 1992-04-03 00:00:00 |
  55. | Ernst | 1992-04-03 00:00:00 |
  56. +-----------+---------------------+
  57. 5 rows in set (0.00 sec)

1.27)str_to_date将字符串通过指定的格式转换成日期

  1. mysql> select last_name, hiredate from employees where hiredate = str_to_date('4-3,1992','%c-%d,%Y');
  2. +-----------+---------------------+
  3. | last_name | hiredate |
  4. +-----------+---------------------+
  5. | K_ing | 1992-04-03 00:00:00 |
  6. | Kochhar | 1992-04-03 00:00:00 |
  7. | De Haan | 1992-04-03 00:00:00 |
  8. | Hunold | 1992-04-03 00:00:00 |
  9. | Ernst | 1992-04-03 00:00:00 |
  10. +-----------+---------------------+
  11. 5 rows in set (0.00 sec)

1.28)date_format 将日期转换为字符串

  1. mysql> select date_format(now(),'%Y年%c月%d日');
  2. +--------------------------------------+
  3. | date_format(now(),'%Y年%c月%d日') |
  4. +--------------------------------------+
  5. | 2021608 |
  6. +--------------------------------------+
  7. 1 row in set (0.00 sec)

6.流程控制函数

1)if函数

  1. mysql> select if (10>5,'大','小');
  2. +-----------------------+
  3. | if (10>5,'大','小') |
  4. +-----------------------+
  5. | |
  6. +-----------------------+
  7. 1 row in set (0.00 sec)
  8. mysql> select if (10<5,'大','小');
  9. +-----------------------+
  10. | if (10<5,'大','小') |
  11. +-----------------------+
  12. | |
  13. +-----------------------+
  14. 1 row in set (0.00 sec)
  15. mysql> select last_name, commission_pct,if(commission_pct is null,'没奖金,呵呵','有奖金,哈哈') 备注 from employees;
  16. +-------------+----------------+--------------------+
  17. | last_name | commission_pct | 备注 |
  18. +-------------+----------------+--------------------+
  19. | K_ing | NULL | 没奖金,呵呵 |
  20. | Kochhar | NULL | 没奖金,呵呵 |
  21. +-------------+----------------+--------------------+

2)case函数
1)语法:
case 要判断的字段或表达式
when 常量1 then 要显示的值1或语句1;
when 常量2 then 要显示的值2或语句2;

else 要显示的值n或语句n;
end

案例:查询员工的工资,要求:
部门号=30,显示的工资为1.1倍
部门号=40,显示的工资为1.2倍
部门号=50,显示的工资为1.3倍
其他部门,显示的工资为原工资

  1. mysql> select salary as 原工资,department_id ,
  2. -> case department_id
  3. -> when 30 then salary*1.1
  4. -> when 40 then salary*1.2
  5. -> when 50 then salary*1.3
  6. -> else salary
  7. -> end as 新工资
  8. -> from employees;
  9. +-----------+---------------+-----------+
  10. | 原工资 | department_id | 新工资 |
  11. +-----------+---------------+-----------+
  12. | 24000.00 | 90 | 24000.00 |
  13. | 17000.00 | 90 | 17000.00 |
  14. | 17000.00 | 90 | 17000.00 |
  15. +-----------+---------------+-----------+

2)
case
when 条件1 then 要显示的值1或语句1
when 条件2 then 要显示的值2或语句2

else 要显示的值n或语句n
end
案例:查询员工的工资情况:
如果工资>20000,显示A级别
如果工资>15000,显示B级别
如果工资>10000,显示C级别
否则,显示D级别

  1. mysql> select salary,
  2. case
  3. when salary>20000 then 'A'
  4. when salary >15000 then 'B'
  5. when salary >10000 then 'C'
  6. else 'D'
  7. end as 工资级别
  8. from employees;
  9. +----------+--------------+
  10. | salary | 工资级别 |
  11. +----------+--------------+
  12. | 24000.00 | A |
  13. | 17000.00 | B |
  14. | 17000.00 | B |
  15. | 9000.00 | D |
  16. | 6000.00 | D |

7. 分组函数

功能:用作统计使用,又称为聚合函数或统计函数或组函数
分类:
sum求和,avg平均值,max最大值,min最小值,count计算个数

1)求工资相关值

  1. mysql> select sum(salary) from employees;
  2. +-------------+
  3. | sum(salary) |
  4. +-------------+
  5. | 691400.00 |
  6. +-------------+
  7. 1 row in set (0.00 sec)
  8. mysql> select avg(salary) from employees;
  9. +-------------+
  10. | avg(salary) |
  11. +-------------+
  12. | 6461.682243 |
  13. +-------------+
  14. 1 row in set (0.00 sec)
  15. mysql> select min(salary) from employees;
  16. +-------------+
  17. | min(salary) |
  18. +-------------+
  19. | 2100.00 |
  20. +-------------+
  21. 1 row in set (0.00 sec)
  22. mysql> select max(salary) from employees;
  23. +-------------+
  24. | max(salary) |
  25. +-------------+
  26. | 24000.00 |
  27. +-------------+
  28. 1 row in set (0.00 sec)

特点:
1)sum、avg一般用于处理数值型
2)max、min、count可以处理任何类型

计算日期查:

  1. mysql> select datediff(now(),'1991-09-17');
  2. +------------------------------+
  3. | datediff(now(),'1991-09-17') |
  4. +------------------------------+
  5. | 10857 |
  6. +------------------------------+
  7. 1 row in set (0.00 sec)

8.分组查询

8.1 语法

  1. select 分组函数,列
  2. from
  3. where 筛选条件
  4. group by 分组的列表
  5. order by 字句
  6. 注意:查询列表必须特殊,要求是分组函数和group by 后出现的字段

1)查询每个工种的最高工资

  1. mysql> select max(salary),job_id from employees group by job_id;
  2. +-------------+------------+
  3. | max(salary) | job_id |
  4. +-------------+------------+
  5. | 8300.00 | AC_ACCOUNT |
  6. | 12000.00 | AC_MGR |
  7. | 4400.00 | AD_ASST |
  8. | 24000.00 | AD_PRES |
  9. | 17000.00 | AD_VP |
  10. | 9000.00 | FI_ACCOUNT |
  11. | 12000.00 | FI_MGR |
  12. | 6500.00 | HR_REP |
  13. | 9000.00 | IT_PROG |
  14. | 13000.00 | MK_MAN |
  15. | 6000.00 | MK_REP |
  16. | 10000.00 | PR_REP |
  17. | 3100.00 | PU_CLERK |
  18. | 11000.00 | PU_MAN |
  19. | 14000.00 | SA_MAN |
  20. | 11500.00 | SA_REP |
  21. | 4200.00 | SH_CLERK |
  22. | 3600.00 | ST_CLERK |
  23. | 8200.00 | ST_MAN |
  24. +-------------+------------+
  25. 19 rows in set (0.00 sec)

2)查询每个区域的部门个数

  1. mysql> select count(*), location_id from departments group by location_id;
  2. +----------+-------------+
  3. | count(*) | location_id |
  4. +----------+-------------+
  5. | 1 | 1400 |
  6. | 1 | 1500 |
  7. | 21 | 1700 |
  8. | 1 | 1800 |
  9. | 1 | 2400 |
  10. | 1 | 2500 |
  11. | 1 | 2700 |
  12. +----------+-------------+
  13. 7 rows in set (0.00 sec)

3)查询邮箱中包含e字符的,每个部门的平均工资

  1. mysql> select department_id,avg(salary) from employees
  2. where email like '%e%' group by department_id;
  3. +---------------+--------------+
  4. | department_id | avg(salary) |
  5. +---------------+--------------+
  6. | 10 | 4400.000000 |
  7. | 20 | 13000.000000 |
  8. | 30 | 6750.000000 |
  9. | 50 | 3221.052632 |
  10. | 60 | 5100.000000 |
  11. | 70 | 10000.000000 |
  12. | 80 | 9781.250000 |
  13. | 90 | 17000.000000 |
  14. | 100 | 9733.333333 |
  15. | 110 | 8300.000000 |
  16. +---------------+--------------+
  17. 10 rows in set (0.00 sec)

4) 查询有奖金的每个领导手下员工的最高工资

  1. mysql> select max(salary),manager_id from employees
  2. where commission_pct is not null group by manager_id;
  3. +-------------+------------+
  4. | max(salary) | manager_id |
  5. +-------------+------------+
  6. | 14000.00 | 100 |
  7. | 10000.00 | 145 |
  8. | 10000.00 | 146 |
  9. | 10500.00 | 147 |
  10. | 11500.00 | 148 |
  11. | 11000.00 | 149 |
  12. +-------------+------------+
  13. 6 rows in set (0.00 sec)

5)添加复杂的筛选条件
查询哪个部门的员工个数>2
思路:先查询每个部门的员工个数,根据查询结果再次筛选

  1. mysql> select count(*),department_id from employees
  2. group by department_id having count(*)>2;
  3. +----------+---------------+
  4. | count(*) | department_id |
  5. +----------+---------------+
  6. | 6 | 30 |
  7. | 45 | 50 |
  8. | 5 | 60 |
  9. | 34 | 80 |
  10. | 3 | 90 |
  11. | 6 | 100 |
  12. +----------+---------------+
  13. 6 rows in set (0.00 sec)

6)查询每个工种有奖金的员工的最高工资>12K的工种编号和最高工资

  1. mysql> select job_id,max(salary) from employees
  2. where commission_pct is not null
  3. group by job_id
  4. having max(salary) >12000;
  5. +--------+-------------+
  6. | job_id | max(salary) |
  7. +--------+-------------+
  8. | SA_MAN | 14000.00 |
  9. +--------+-------------+
  10. 1 row in set (0.00 sec)

7)查询领导编号>102的每个领导手下的最低工资>5000的领导编号是哪个,以及其最低工资

  1. mysql> select manager_id,min(salary) from employees
  2. where manager_id >102
  3. group by manager_id
  4. having min(salary)>5000;
  5. +------------+-------------+
  6. | manager_id | min(salary) |
  7. +------------+-------------+
  8. | 108 | 6900.00 |
  9. | 145 | 7000.00 |
  10. | 146 | 7000.00 |
  11. | 147 | 6200.00 |
  12. | 148 | 6100.00 |
  13. | 149 | 6200.00 |
  14. | 201 | 6000.00 |
  15. | 205 | 8300.00 |
  16. +------------+-------------+
  17. 8 rows in set (0.00 sec)

8)查询每个部门每个工种的平均工资

  1. mysql> select department_id, job_id,avg(salary) from employees
  2. group by department_id, job_id;
  3. +---------------+------------+--------------+
  4. | department_id | job_id | avg(salary) |
  5. +---------------+------------+--------------+
  6. | NULL | SA_REP | 7000.000000 |
  7. | 10 | AD_ASST | 4400.000000 |
  8. | 20 | MK_MAN | 13000.000000 |
  9. | 20 | MK_REP | 6000.000000 |
  10. | 30 | PU_CLERK | 2780.000000 |
  11. | 30 | PU_MAN | 11000.000000 |
  12. | 40 | HR_REP | 6500.000000 |
  13. | 50 | SH_CLERK | 3215.000000 |
  14. | 50 | ST_CLERK | 2785.000000 |
  15. | 50 | ST_MAN | 7280.000000 |
  16. | 60 | IT_PROG | 5760.000000 |
  17. | 70 | PR_REP | 10000.000000 |
  18. | 80 | SA_MAN | 12200.000000 |
  19. | 80 | SA_REP | 8396.551724 |
  20. | 90 | AD_PRES | 24000.000000 |
  21. | 90 | AD_VP | 17000.000000 |
  22. | 100 | FI_ACCOUNT | 7920.000000 |
  23. | 100 | FI_MGR | 12000.000000 |
  24. | 110 | AC_ACCOUNT | 8300.000000 |
  25. | 110 | AC_MGR | 12000.000000 |
  26. +---------------+------------+--------------+
  27. 20 rows in set (0.00 sec)

9)查询每个部门每个工种的平均工资,并且按平均工资的高低显示

  1. mysql> select department_id, job_id,avg(salary) from employees
  2. group by department_id, job_id
  3. order by avg(salary) desc;
  4. +---------------+------------+--------------+
  5. | department_id | job_id | avg(salary) |
  6. +---------------+------------+--------------+
  7. | 90 | AD_PRES | 24000.000000 |
  8. | 90 | AD_VP | 17000.000000 |
  9. | 20 | MK_MAN | 13000.000000 |
  10. | 80 | SA_MAN | 12200.000000 |
  11. | 110 | AC_MGR | 12000.000000 |
  12. | 100 | FI_MGR | 12000.000000 |
  13. | 30 | PU_MAN | 11000.000000 |
  14. | 70 | PR_REP | 10000.000000 |
  15. | 80 | SA_REP | 8396.551724 |
  16. | 110 | AC_ACCOUNT | 8300.000000 |
  17. | 100 | FI_ACCOUNT | 7920.000000 |
  18. | 50 | ST_MAN | 7280.000000 |
  19. | NULL | SA_REP | 7000.000000 |
  20. | 40 | HR_REP | 6500.000000 |
  21. | 20 | MK_REP | 6000.000000 |
  22. | 60 | IT_PROG | 5760.000000 |
  23. | 10 | AD_ASST | 4400.000000 |
  24. | 50 | SH_CLERK | 3215.000000 |
  25. | 50 | ST_CLERK | 2785.000000 |
  26. | 30 | PU_CLERK | 2780.000000 |
  27. +---------------+------------+--------------+
  28. 20 rows in set (0.00 sec)

9.连接查询

9.1 简介

连接查询又称为多表查询
笛卡尔乘积现象:表1有m行,表2有n行,结果=m*n行
分类:
按年代分类:
sql92标准
sql99标准
按功能分类:
内连接:
等值连接
非等值连接
自连接

外连接:
左外
右外
全外

交叉连接:

9.2 案例

1)查询女神名和对应的男神名

  1. mysql> select name, boyName from beauty ,boys where beauty.boyfriend_id = boys.id;
  2. +------------+-----------+
  3. | name | boyName |
  4. +------------+-----------+
  5. | Angelababy | 黄晓明 |
  6. | 热巴 | 鹿晗 |
  7. | 周芷若 | 张无忌 |
  8. | 小昭 | 张无忌 |
  9. | 王语嫣 | 段誉 |
  10. | 赵敏 | 张无忌 |
  11. +------------+-----------+
  12. 6 rows in set (0.01 sec)

2)查询员工名对应的部门名

  1. mysql> select last_name, department_name from employees,departments
  2. where employees.department_id = departments.department_id limit 10 ;
  3. +------------+-----------------+
  4. | last_name | department_name |
  5. +------------+-----------------+
  6. | Whalen | Adm |
  7. | Hartstein | Mar |
  8. | Fay | Mar |
  9. | Raphaely | Pur |
  10. | Khoo | Pur |
  11. | Baida | Pur |
  12. | Tobias | Pur |
  13. | Himuro | Pur |
  14. | Colmenares | Pur |
  15. | Mavris | Hum |
  16. +------------+-----------------+
  17. 10 rows in set (0.00 sec)

3)查询员工名,工种号,工种名

  1. mysql> select e.last_name,e.job_id,j.job_title from employees e ,jobs j
  2. where e.job_id = j.job_id;
  3. +-------------+------------+---------------------------------+
  4. | last_name | job_id | job_title |
  5. +-------------+------------+---------------------------------+
  6. | Gietz | AC_ACCOUNT | Public Accountant |
  7. | Higgins | AC_MGR | Accounting Manager |
  8. | Whalen | AD_ASST | Administration Assistant |
  9. | K_ing | AD_PRES | President |
  10. | Kochhar | AD_VP | Administration Vice President |
  11. +-------------+------------+---------------------------------+

4)查询有奖金的员工名、部门名

  1. mysql> select e.last_name , d.department_name,e.commission_pct
  2. from employees e ,departments d
  3. where e.department_id = d.department_id and e.commission_pct is not null limit 10;
  4. +-----------+-----------------+----------------+
  5. | last_name | department_name | commission_pct |
  6. +-----------+-----------------+----------------+
  7. | Russell | Sal | 0.40 |
  8. | Partners | Sal | 0.30 |
  9. | Errazuriz | Sal | 0.30 |
  10. | Cambrault | Sal | 0.30 |
  11. | Zlotkey | Sal | 0.20 |
  12. | Tucker | Sal | 0.30 |
  13. | Bernstein | Sal | 0.25 |
  14. | Hall | Sal | 0.25 |
  15. | Olsen | Sal | 0.20 |
  16. | Cambrault | Sal | 0.20 |
  17. +-----------+-----------------+----------------+
  18. 10 rows in set (0.00 sec)

5)查询城市名中第二个字符为o的部门名和城市名

  1. mysql> select d.department_name ,l.city from departments d, locations l
  2. where d.location_id = l.location_id
  3. and d.department_name like '_o%';
  4. +-----------------+---------+
  5. | department_name | city |
  6. +-----------------+---------+
  7. | Cor | Seattle |
  8. | Con | Seattle |
  9. | Con | Seattle |
  10. | Con | Seattle |
  11. | NOC | Seattle |
  12. | Gov | Seattle |
  13. +-----------------+---------+
  14. 6 rows in set (0.00 sec)

6)查询每个城市的部门个数

  1. mysql> select count(*), city from departments d,locations l
  2. where d.location_id = l.location_id group by city;
  3. +----------+---------------------+
  4. | count(*) | city |
  5. +----------+---------------------+
  6. | 1 | London |
  7. | 1 | Munich |
  8. | 1 | Oxford |
  9. | 21 | Seattle |
  10. | 1 | South San Francisco |
  11. | 1 | Southlake |
  12. | 1 | Toronto |
  13. +----------+---------------------+
  14. 7 rows in set (0.00 sec)

7)查询有奖金的每个部门的部门名和部门的领导编号和该部门的最低工资

  1. mysql> select d.department_name,e.manager_id,min(e.salary) from departments d ,employees e
  2. where e.department_id = d .department_id and e.commission_pct is not null
  3. group by department_name,manager_id;
  4. +-----------------+------------+---------------+
  5. | department_name | manager_id | min(e.salary) |
  6. +-----------------+------------+---------------+
  7. | Sal | 100 | 10500.00 |
  8. | Sal | 145 | 7000.00 |
  9. | Sal | 146 | 7000.00 |
  10. | Sal | 147 | 6200.00 |
  11. | Sal | 148 | 6100.00 |
  12. | Sal | 149 | 6200.00 |
  13. +-----------------+------------+---------------+
  14. 6 rows in set (0.00 sec)

8)查询每个工种的工种名和员工的个数,并且按员工个数降序

  1. mysql> select j.job_title ,count(*) from employees e ,jobs j
  2. where e.job_id = j.job_id
  3. group by job_title
  4. order by count(*) desc;
  5. +---------------------------------+----------+
  6. | job_title | count(*) |
  7. +---------------------------------+----------+
  8. | Sales Representative | 30 |
  9. | Shipping Clerk | 20 |
  10. | Stock Clerk | 20 |
  11. | Purchasing Clerk | 5 |
  12. | Stock Manager | 5 |
  13. | Accountant | 5 |
  14. | Programmer | 5 |
  15. | Sales Manager | 5 |
  16. +---------------------------------+----------+

9)查询员工名,部门名和所在的城市

  1. mysql> select e.last_name,d.department_name,l.city from employees e, departments d ,locations l
  2. where e.department_id = d.department_id and d.location_id = l.location_id limit 10 ;
  3. +------------+-----------------+---------+
  4. | last_name | department_name | city |
  5. +------------+-----------------+---------+
  6. | Whalen | Adm | Seattle |
  7. | Hartstein | Mar | Toronto |
  8. | Fay | Mar | Toronto |
  9. | Raphaely | Pur | Seattle |
  10. | Khoo | Pur | Seattle |
  11. | Baida | Pur | Seattle |
  12. | Tobias | Pur | Seattle |
  13. | Himuro | Pur | Seattle |
  14. | Colmenares | Pur | Seattle |
  15. | Mavris | Hum | London |
  16. +------------+-----------------+---------+
  17. 10 rows in set (0.00 sec)

非等值连接:
1)查询员工的工资和工资级别

  1. mysql> select salary , grade_level from employees e ,job_grades jg
  2. where salary between lowest_sal and highest_sal limit 10;
  3. +----------+-------------+
  4. | salary | grade_level |
  5. +----------+-------------+
  6. | 24000.00 | E |
  7. | 17000.00 | E |
  8. | 17000.00 | E |
  9. | 9000.00 | C |
  10. | 6000.00 | C |
  11. | 4800.00 | B |
  12. | 4800.00 | B |
  13. | 4200.00 | B |
  14. | 12000.00 | D |
  15. | 9000.00 | C |
  16. +----------+-------------+
  17. 10 rows in set (0.00 sec)

自连接:
1)查询员工名和上级名

  1. mysql> select e.employee_id,e.last_name, m.employee_id,m.last_name from employees e ,employees m
  2. where e.manager_id = m.employee_id limit 10 ;
  3. +-------------+-----------+-------------+-----------+
  4. | employee_id | last_name | employee_id | last_name |
  5. +-------------+-----------+-------------+-----------+
  6. | 101 | Kochhar | 100 | K_ing |
  7. | 102 | De Haan | 100 | K_ing |
  8. | 103 | Hunold | 102 | De Haan |
  9. | 104 | Ernst | 103 | Hunold |
  10. | 105 | Austin | 103 | Hunold |
  11. | 106 | Pataballa | 103 | Hunold |
  12. | 107 | Lorentz | 103 | Hunold |
  13. | 108 | Greenberg | 101 | Kochhar |
  14. | 109 | Faviet | 108 | Greenberg |
  15. | 110 | Chen | 108 | Greenberg |
  16. +-------------+-----------+-------------+-----------+
  17. 10 rows in set (0.00 sec)

9.3 sql99语法

  1. 语法:
  2. select 查询列表
  3. from 1 别名 [连接类型]
  4. join 2 别名
  5. on 连接条件
  6. where 筛选条件
  7. group by 分组调教
  8. having 筛选条件
  9. order by 排序条件
  10. 分类:
  11. 内连接:inner
  12. 外连接:
  13. 左外:left [outer]
  14. 右外:right [outer]
  15. 全外:full [outer]
  16. 交叉:cross

内连接

  1. 语法:
  2. select 查询列表
  3. from 1 别名
  4. inner join 2 别名
  5. on 连接条件
  6. 分类:
  7. 等值
  8. 非等值
  9. 自连接

1)查询员工名、部门名

  1. mysql> select e.last_name,d.department_name from employees e
  2. inner join departments d
  3. on e.department_id = d.department_id limit 10 ;
  4. +------------+-----------------+
  5. | last_name | department_name |
  6. +------------+-----------------+
  7. | Whalen | Adm |
  8. | Hartstein | Mar |
  9. | Fay | Mar |
  10. | Raphaely | Pur |
  11. | Khoo | Pur |
  12. | Baida | Pur |
  13. | Tobias | Pur |
  14. | Himuro | Pur |
  15. | Colmenares | Pur |
  16. | Mavris | Hum |
  17. +------------+-----------------+
  18. 10 rows in set (0.00 sec)

2)查询名字中包含e的员工名和工种名

  1. mysql> select e.last_name, j.job_title from employees e
  2. inner join jobs j on e.job_id = j.job_id
  3. where e.last_name like '%e%' limit 10;
  4. +------------+-------------------------------+
  5. | last_name | job_title |
  6. +------------+-------------------------------+
  7. | De Haan | Administration Vice President |
  8. | Ernst | Programmer |
  9. | Lorentz | Programmer |
  10. | Greenberg | Finance Manager |
  11. | Faviet | Accountant |
  12. | Chen | Accountant |
  13. | Raphaely | Purchasing Manager |
  14. | Colmenares | Purchasing Clerk |
  15. | Weiss | Stock Manager |
  16. | Nayer | Stock Clerk |
  17. +------------+-------------------------------+
  18. 10 rows in set (0.00 sec)

3)查询部门个数>3的城市名和部门个数

  1. mysql> select l.city, count(*) from departments d
  2. inner join locations l
  3. on l.location_id = d.location_id
  4. group by l.city having count(*)>3;
  5. +---------+----------+
  6. | city | count(*) |
  7. +---------+----------+
  8. | Seattle | 21 |
  9. +---------+----------+
  10. 1 row in set (0.00 sec)

4)查询哪个部门的员工个数>3的部门名和员工个数,并按个数降序;

  1. mysql> select d.department_name,count(*) from employees e
  2. inner join departments d
  3. on e.department_id = d.department_id
  4. group by department_name having count(*) >3
  5. order by count(*) desc;
  6. +-----------------+----------+
  7. | department_name | count(*) |
  8. +-----------------+----------+
  9. | Shi | 45 |
  10. | Sal | 34 |
  11. | Fin | 6 |
  12. | Pur | 6 |
  13. | IT | 5 |
  14. +-----------------+----------+
  15. 5 rows in set (0.00 sec)

5)查询员工名、部门名、工种名并按部门降序

  1. mysql> select e.last_name, d.department_name,j.job_title from employees e
  2. inner join departments d on e.department_id = d.department_id
  3. inner join jobs j on e.job_id = j.job_id
  4. order by department_name desc limit 10;
  5. +-----------+-----------------+----------------+
  6. | last_name | department_name | job_title |
  7. +-----------+-----------------+----------------+
  8. | Geoni | Shi | Shipping Clerk |
  9. | Patel | Shi | Stock Clerk |
  10. | Nayer | Shi | Stock Clerk |
  11. | Bull | Shi | Shipping Clerk |
  12. | Davies | Shi | Stock Clerk |
  13. | Landry | Shi | Stock Clerk |
  14. | Cabrio | Shi | Shipping Clerk |
  15. | Vargas | Shi | Stock Clerk |
  16. | Bissot | Shi | Stock Clerk |
  17. | Dilly | Shi | Shipping Clerk |
  18. +-----------+-----------------+----------------+
  19. 10 rows in set (0.00 sec)

非等值连接
1)查询员工的工资级别

  1. mysql> select salary,grade_level
  2. from employees e
  3. inner join job_grades g
  4. on e.salary between g.lowest_sal and g.highest_sal;
  5. +----------+-------------+
  6. | salary | grade_level |
  7. +----------+-------------+
  8. | 24000.00 | E |
  9. | 17000.00 | E |
  10. | 17000.00 | E |
  11. | 9000.00 | C |
  12. | 6000.00 | C |
  13. | 4800.00 | B |
  14. | 4800.00 | B |
  15. | 4200.00 | B |
  16. | 12000.00 | D |
  17. | 9000.00 | C |
  18. | 8200.00 | C |
  19. | 7700.00 | C |
  20. | 7800.00 | C |
  21. | 6900.00 | C |
  22. | 11000.00 | D |
  23. +----------+-------------+

2)查询工资级别个数>2的个数,并且按工资级别降序

  1. mysql> select g.grade_level,count(*)
  2. from employees e
  3. inner join job_grades g on e.salary
  4. between g.lowest_sal and g.highest_sal
  5. group by g.grade_level
  6. having count(*) > 20 order by g.grade_level desc;
  7. +-------------+----------+
  8. | grade_level | count(*) |
  9. +-------------+----------+
  10. | C | 38 |
  11. | B | 26 |
  12. | A | 24 |
  13. +-------------+----------+
  14. 3 rows in set (0.00 sec)

自连接
1)查询员工的名字,上级的名字

  1. mysql> select e.employee_id,e.last_name, m.employee_id,m.last_name from employees e
  2. inner join employees m
  3. on e.manager_id = m.employee_id limit 10 ;
  4. +-------------+-----------+-------------+-----------+
  5. | employee_id | last_name | employee_id | last_name |
  6. +-------------+-----------+-------------+-----------+
  7. | 101 | Kochhar | 100 | K_ing |
  8. | 102 | De Haan | 100 | K_ing |
  9. | 103 | Hunold | 102 | De Haan |
  10. | 104 | Ernst | 103 | Hunold |
  11. | 105 | Austin | 103 | Hunold |
  12. | 106 | Pataballa | 103 | Hunold |
  13. | 107 | Lorentz | 103 | Hunold |
  14. | 108 | Greenberg | 101 | Kochhar |
  15. | 109 | Faviet | 108 | Greenberg |
  16. | 110 | Chen | 108 | Greenberg |
  17. +-------------+-----------+-------------+-----------+
  18. 10 rows in set (0.00 sec)

外连接
应用场景:用于查询一个表中有,另一个表中没有的记录

特点:
1.外连接的查询结果为主表中的所有记录
如果从表中有和主表匹配的,则显示匹配的值
如果从表中没有和主表匹配的,则显示null
外连接结果=内连接结果+主表中有而从表中没有的记录

2.左外连接:left join左边的是主表
右外连接:right join右边的是主表
3.左外和右外交换两个表的顺序,可以实现同样的效果

  1. mysql> select b.name from beauty b left outer join boys bo on b.boyfriend_id = bo.id where bo.id is null;
  2. +-----------+
  3. | name |
  4. +-----------+
  5. | 柳岩 |
  6. | 苍老师 |
  7. | 周冬雨 |
  8. | 岳灵珊 |
  9. | 双儿 |
  10. | 夏雪 |
  11. +-----------+
  12. 6 rows in set (0.00 sec)

2)查询哪个部门没有员工

  1. mysql> select d.* , e.employee_id from departments d
  2. left outer join employees e on d.department_id = e.department_id
  3. where e.employee_id is null;
  4. +---------------+-----------------+------------+-------------+-------------+
  5. | department_id | department_name | manager_id | location_id | employee_id |
  6. +---------------+-----------------+------------+-------------+-------------+
  7. | 120 | Tre | NULL | 1700 | NULL |
  8. | 130 | Cor | NULL | 1700 | NULL |
  9. | 140 | Con | NULL | 1700 | NULL |
  10. | 150 | Sha | NULL | 1700 | NULL |
  11. | 160 | Ben | NULL | 1700 | NULL |
  12. | 170 | Man | NULL | 1700 | NULL |
  13. | 180 | Con | NULL | 1700 | NULL |
  14. | 190 | Con | NULL | 1700 | NULL |
  15. | 200 | Ope | NULL | 1700 | NULL |
  16. | 210 | IT | NULL | 1700 | NULL |
  17. | 220 | NOC | NULL | 1700 | NULL |
  18. | 230 | IT | NULL | 1700 | NULL |
  19. | 240 | Gov | NULL | 1700 | NULL |
  20. | 250 | Ret | NULL | 1700 | NULL |
  21. | 260 | Rec | NULL | 1700 | NULL |
  22. | 270 | Pay | NULL | 1700 | NULL |
  23. +---------------+-----------------+------------+-------------+-------------+
  24. 16 rows in set (0.00 sec)

10 子查询

含义:出现在其他语句中的select语句,称为子查询或内查询,外部的查询语句称为主查询或外查询
分类:
按子查询出现的位置:
select 后面:仅仅支持标量子查询
from 后面:支持表子查询
where 或having后面:支持标量子查询,列子查询,行子查询
exists后面:支持表子查询

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

标量子查询
1)谁的工资比Abel高

  1. mysql> select last_name,salary from employees where salary > (select salary from employees where last_name = 'Abeel');
  2. +-----------+----------+
  3. | last_name | salary |
  4. +-----------+----------+
  5. | K_ing | 24000.00 |
  6. | Kochhar | 17000.00 |
  7. | De Haan | 17000.00 |
  8. | Greenberg | 12000.00 |
  9. | Russell | 14000.00 |
  10. | Partners | 13500.00 |
  11. | Errazuriz | 12000.00 |
  12. | Ozer | 11500.00 |
  13. | Hartstein | 13000.00 |
  14. | Higgins | 12000.00 |
  15. +-----------+----------+
  16. 10 rows in set (0.00 sec)

2)返回job_id与141号员工相同,salary比143号员工多的员工姓名,job_id和工资

  1. mysql> select last_name,job_id,salary from employees where salary >(select salary from employees where employee_did =143) and job_id =(select job_id from employees where employee_id =141);
  2. +-------------+----------+---------+
  3. | last_name | job_id | salary |
  4. +-------------+----------+---------+
  5. | Nayer | ST_CLERK | 3200.00 |
  6. | Mikkilineni | ST_CLERK | 2700.00 |
  7. | Bissot | ST_CLERK | 3300.00 |
  8. | Atkinson | ST_CLERK | 2800.00 |
  9. | Mallin | ST_CLERK | 3300.00 |
  10. | Rogers | ST_CLERK | 2900.00 |
  11. | Ladwig | ST_CLERK | 3600.00 |
  12. | Stiles | ST_CLERK | 3200.00 |
  13. | Seo | ST_CLERK | 2700.00 |
  14. | Rajs | ST_CLERK | 3500.00 |
  15. | Davies | ST_CLERK | 3100.00 |
  16. +-------------+----------+---------+
  17. 11 rows in set (0.01 sec)

3)返回公司工资最少的员工的last_name,job_id,和salary

  1. mysql> select last_name,job_id,salary from employees where salary =(select min(salary) from employees);
  2. +-----------+----------+---------+
  3. | last_name | job_id | salary |
  4. +-----------+----------+---------+
  5. | Olson | ST_CLERK | 2100.00 |
  6. +-----------+----------+---------+
  7. 1 row in set (0.00 sec)

4)查询最低工资大于50号部门最低工资的部门id和其最低工资

  1. mysql> select min(salary),department_id from employees
  2. where salary>(select min(salary) from employees where department_id=50)
  3. group by department_id;
  4. +-------------+---------------+
  5. | min(salary) | department_id |
  6. +-------------+---------------+
  7. | 7000.00 | NULL |
  8. | 4400.00 | 10 |
  9. | 6000.00 | 20 |
  10. | 2500.00 | 30 |
  11. | 6500.00 | 40 |
  12. | 2200.00 | 50 |
  13. | 4200.00 | 60 |
  14. | 10000.00 | 70 |
  15. | 6100.00 | 80 |
  16. | 17000.00 | 90 |
  17. | 6900.00 | 100 |
  18. | 8300.00 | 110 |
  19. +-------------+---------------+
  20. 12 rows in set (0.00 sec)

列子查询
1)查询location_id是1400或1700的部门中的所有员工姓名

  1. mysql> select last_name from employees
  2. where department_id
  3. in (select distinct department_id from departments whhere location_id in (1400,1700));
  4. +------------+
  5. | last_name |
  6. +------------+
  7. | Hunold |
  8. | Ernst |
  9. | Austin |
  10. | Pataballa |
  11. | Lorentz |
  12. | Whalen |
  13. | Raphaely |
  14. | Khoo |
  15. | Baida |
  16. | Tobias |
  17. | Himuro |
  18. | Colmenares |
  19. | K_ing |
  20. | Kochhar |
  21. | De Haan |
  22. | Greenberg |
  23. | Faviet |
  24. | Chen |
  25. | Sciarra |
  26. | Urman |
  27. | Popp |
  28. | Higgins |
  29. | Gietz |
  30. +------------+
  31. 23 rows in set (0.00 sec)

子查询案例
1)查询和Zlotkey相同部门的员工姓名和工资

  1. mysql> select last_name,salary from employees
  2. where department_id=(select department_id from employees where last_name = 'Zlotkey');
  3. +------------+----------+
  4. | last_name | salary |
  5. +------------+----------+
  6. | Russell | 14000.00 |
  7. | Partners | 13500.00 |
  8. | Errazuriz | 12000.00 |
  9. | Cambrault | 11000.00 |
  10. | Zlotkey | 10500.00 |
  11. +------------+----------+

2)查询工资比公司平均工资高的员工的员工号,姓名和工资

  1. mysql> select employee_id,last_name,salary from employees where salary>(select avg(salary) from employees);
  2. +-------------+------------+----------+
  3. | employee_id | last_name | salary |
  4. +-------------+------------+----------+
  5. | 100 | K_ing | 24000.00 |
  6. | 101 | Kochhar | 17000.00 |
  7. | 102 | De Haan | 17000.00 |
  8. | 103 | Hunold | 9000.00 |
  9. | 108 | Greenberg | 12000.00 |
  10. +-------------+------------+----------+

11 分页查询

应用场景:当要显示的数据,一页显示不完,需要分页提交sql请求。
语法:
select 查询列表
from 表
join type join 表2
on 连接条件
where 筛选条件
group by 分组字段
having 分组后的筛选
order by 排序的字段
limit offset ,size
offset 要显示条目的其实索引
起始索引从0开始
SIZE要显示的条目个数

select 查询列表 from 表 limit (page -1)* size,size;

1)案例:查询前5条员工信息

  1. mysql> select * from employees limit 0,5;
  2. +-------------+------------+-----------+----------+--------------+---------+----------+----------------+------------+---------------+---------------------+
  3. | employee_id | first_name | last_name | email | phone_number | job_id | salary | commission_pct | manager_id | department_id | hiredate |
  4. +-------------+------------+-----------+----------+--------------+---------+----------+----------------+------------+---------------+---------------------+
  5. | 100 | Steven | K_ing | SKING | 515.123.4567 | AD_PRES | 24000.00 | NULL | NULL | 90 | 1992-04-03 00:00:00 |
  6. | 101 | Neena | Kochhar | NKOCHHAR | 515.123.4568 | AD_VP | 17000.00 | NULL | 100 | 90 | 1992-04-03 00:00:00 |
  7. | 102 | Lex | De Haan | LDEHAAN | 515.123.4569 | AD_VP | 17000.00 | NULL | 100 | 90 | 1992-04-03 00:00:00 |
  8. | 103 | Alexander | Hunold | AHUNOLD | 590.423.4567 | IT_PROG | 9000.00 | NULL | 102 | 60 | 1992-04-03 00:00:00 |
  9. | 104 | Bruce | Ernst | BERNST | 590.423.4568 | IT_PROG | 6000.00 | NULL | 103 | 60 | 1992-04-03 00:00:00 |
  10. +-------------+------------+-----------+----------+--------------+---------+----------+----------------+------------+---------------+---------------------+
  11. 5 rows in set (0.00 sec)

2)查询第11-第25条

  1. mysql> select * from employees limit 10,15;
  2. +-------------+-------------+------------+----------+--------------+------------+----------+----------------+------------+---------------+---------------------+
  3. | employee_id | first_name | last_name | email | phone_number | job_id | salary | commission_pct | manager_id | department_id | hiredate |
  4. +-------------+-------------+------------+----------+--------------+------------+----------+----------------+------------+---------------+---------------------+
  5. | 110 | John | Chen | JCHEN | 515.124.4269 | FI_ACCOUNT | 8200.00 | NULL | 108 | 100 | 2000-09-09 00:00:00 |
  6. | 111 | Ismael | Sciarra | ISCIARRA | 515.124.4369 | FI_ACCOUNT | 7700.00 | NULL | 108 | 100 | 2000-09-09 00:00:00 |
  7. | 112 | Jose Manuel | Urman | JMURMAN | 515.124.4469 | FI_ACCOUNT | 7800.00 | NULL | 108 | 100 | 2000-09-09 00:00:00 |
  8. | 113 | Luis | Popp | LPOPP | 515.124.4567 | FI_ACCOUNT | 6900.00 | NULL | 108 | 100 | 2000-09-09 00:00:00 |
  9. | 114 | Den | Raphaely | DRAPHEAL | 515.127.4561 | PU_MAN | 11000.00 | NULL | 100 | 30 | 2000-09-09 00:00:00 |
  10. | 115 | Alexander | Khoo | AKHOO | 515.127.4562 | PU_CLERK | 3100.00 | NULL | 114 | 30 | 2000-09-09 00:00:00 |
  11. | 116 | Shelli | Baida | SBAIDA | 515.127.4563 | PU_CLERK | 2900.00 | NULL | 114 | 30 | 2000-09-09 00:00:00 |
  12. | 117 | Sigal | Tobias | STOBIAS | 515.127.4564 | PU_CLERK | 2800.00 | NULL | 114 | 30 | 2000-09-09 00:00:00 |
  13. | 118 | Guy | Himuro | GHIMURO | 515.127.4565 | PU_CLERK | 2600.00 | NULL | 114 | 30 | 2000-09-09 00:00:00 |
  14. | 119 | Karen | Colmenares | KCOLMENA | 515.127.4566 | PU_CLERK | 2500.00 | NULL | 114 | 30 | 2000-09-09 00:00:00 |
  15. | 120 | Matthew | Weiss | MWEISS | 650.123.1234 | ST_MAN | 8000.00 | NULL | 100 | 50 | 2004-02-06 00:00:00 |
  16. | 121 | Adam | Fripp | AFRIPP | 650.123.2234 | ST_MAN | 8200.00 | NULL | 100 | 50 | 2004-02-06 00:00:00 |
  17. | 122 | Payam | Kaufling | PKAUFLIN | 650.123.3234 | ST_MAN | 7900.00 | NULL | 100 | 50 | 2004-02-06 00:00:00 |
  18. | 123 | Shanta | Vollman | SVOLLMAN | 650.123.4234 | ST_MAN | 6500.00 | NULL | 100 | 50 | 2004-02-06 00:00:00 |
  19. | 124 | Kevin | Mourgos | KMOURGOS | 650.123.5234 | ST_MAN | 5800.00 | NULL | 100 | 50 | 2004-02-06 00:00:00 |
  20. +-------------+-------------+------------+----------+--------------+------------+----------+----------------+------------+---------------+---------------------+
  21. 15 rows in set (0.00 sec)

3)有奖金的员工信息,并且工资较高的前10名显示出来

  1. mysql> select * from employees where commission_pct is not null limit 10;
  2. +-------------+-------------+-----------+----------+--------------------+--------+----------+----------------+------------+---------------+---------------------+
  3. | employee_id | first_name | last_name | email | phone_number | job_id | salary | commission_pct | manager_id | department_id | hiredate |
  4. +-------------+-------------+-----------+----------+--------------------+--------+----------+----------------+------------+---------------+---------------------+
  5. | 145 | John | Russell | JRUSSEL | 011.44.1344.429268 | SA_MAN | 14000.00 | 0.40 | 100 | 80 | 2002-12-23 00:00:00 |
  6. | 146 | Karen | Partners | KPARTNER | 011.44.1344.467268 | SA_MAN | 13500.00 | 0.30 | 100 | 80 | 2002-12-23 00:00:00 |
  7. | 147 | Alberto | Errazuriz | AERRAZUR | 011.44.1344.429278 | SA_MAN | 12000.00 | 0.30 | 100 | 80 | 2002-12-23 00:00:00 |
  8. | 148 | Gerald | Cambrault | GCAMBRAU | 011.44.1344.619268 | SA_MAN | 11000.00 | 0.30 | 100 | 80 | 2002-12-23 00:00:00 |
  9. | 149 | Eleni | Zlotkey | EZLOTKEY | 011.44.1344.429018 | SA_MAN | 10500.00 | 0.20 | 100 | 80 | 2002-12-23 00:00:00 |
  10. | 150 | Peter | Tucker | PTUCKER | 011.44.1344.129268 | SA_REP | 10000.00 | 0.30 | 145 | 80 | 2014-03-05 00:00:00 |
  11. | 151 | David | Bernstein | DBERNSTE | 011.44.1344.345268 | SA_REP | 9500.00 | 0.25 | 145 | 80 | 2014-03-05 00:00:00 |
  12. | 152 | Peter | Hall | PHALL | 011.44.1344.478968 | SA_REP | 9000.00 | 0.25 | 145 | 80 | 2014-03-05 00:00:00 |
  13. | 153 | Christopher | Olsen | COLSEN | 011.44.1344.498718 | SA_REP | 8000.00 | 0.20 | 145 | 80 | 2014-03-05 00:00:00 |
  14. | 154 | Nanette | Cambrault | NCAMBRAU | 011.44.1344.987668 | SA_REP | 7500.00 | 0.20 | 145 | 80 | 2014-03-05 00:00:00 |
  15. +-------------+-------------+-----------+----------+--------------------+--------+----------+----------------+------------+---------------+---------------------+
  16. 10 rows in set (0.00 sec)

12 联合查询

union 联合,将多条查询语句的结果合并成一个结果