Study Notes of MySQL 1 —— Query Function

  • DQL (Data Query Language)

[toc]

MySQL 基础

  • MySQL 服务的登录和退出

    1
    2
    3
    4
    5
    6
    # 登录方式一:MySQL 自带客户端
    # 登录方式二:通过 Windows 自带的客户端
    # mysql 【-h 主机名 -p 端口号】 -u用户名 -p密码

    # 退出方式:
    # exit or Ctrl + C
  • MySQL常见命令

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    # 查看当前所有数据库
    # show databases;

    # 打开指定库
    # use 库名

    # 查看当前库的所有表
    # show tables;

    # 查看其他库的所有表
    # show tables from 库名;

    # 创建表
    # create table 库名(
    列名 列类型,
    列名 列类型
    );

    # 查看表结构
    # desc 表名;

    # 查看服务器版本
    # 登录到MySQL
    # select version()

    # 没有登录到MySQL
    # mysql --version
    # mysql -V
  • MySQL 的语法规范

    • 不区分大小写
    • 建议关键字大写,表名列名小写
    • 每条命令最好用分号结尾
    • 每条命令根据需要进行缩进或者换行
    • 注释的方式
      • 单行注释:# 注释文字
      • 单行注释:— 注释文字
      • 多行注释:/ 注释文字 /

DQL (Data Query Language)

1
2
3
4
5
6
7
8
9
# 完整的查询语句
select 查询列表
from 表
join 表2
where 筛选条件
group by 分组列表
having 分组后的筛选
order by 排序列表
limit 偏移,条目数

基础查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 调用的库
USE myemployees;

# 查询列表可以是:表中的字段,常量,表达式,函数
# 的结果是一个虚拟的表格
SELECT 查询列表 FROM 表名;

# 查询表中的单个字段
SELECT last_name FROM employees;

# 查询表中多个字段
SELECT last_name, salary, email FROM employees;

# 查询表中的所有字段
SELECT * FROM employees;

# 查询常量值
SELECT 100;
SELECT 'john';

# 查询表达式
SELECT 100%98;

# 查询函数
SELECT VERSION();

# 起别名
# 方法一
SELECT 100%98 AS 结果;
SELECT last_name AS 姓, first_name AS 名 FROM employees;
# 方法二
SELECT last_name 姓, first_name 名 FROM employee;
# 别名和关键字相同
SELECT last_name AS 'OUT PUT' FROM employee;

# 去重
SELECT DISTINCT department_id FROM employees;

# '+' 的作用
# 仅仅只有一个功能,就是运算符,不能用来操作字符串
# 如果存在字符型,那么就会试图转换成数值型,转换成功,继续做加法
# 如果转换失败,那么字符型就转换成0
# 如果其中一方为 null, 结果返回 null
SELECT last_name + first_name AS 姓名 FROM employee;

# 拼接字段
SELECT CONCAT(last_name, first_name) AS 姓名 FROM employee;

条件查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# 语法
# 先执行 FROM 然后是 WHERE 最后是 SELECT
SELECT
查询列表
FROM
表名
WHERE
筛选条件;

# 分类
# 按照条件表达式筛选,条件运算符: > < = != <> >= <=
# 按照逻辑表达式筛选,逻辑运算符:&& || ! and or not
# 模糊查询:like/between and/in/is null

# 按照条件表达式筛选
SELECT * FROM employees WHERE salary>12000;

SELECT
last_name, department_id
FROM
employees
WHERE
department_id != 90;

# 按照逻辑表达式筛选
SELECT
last_name,
salary
FROM
employees
WHERE
salary >= 10000 AND salary <= 20000;

# 模糊查询
# like 一般和通配符搭配使用
# % 任意多个字符
# - 单个字符
#
SELECT
*
FROM
employees
WHERE last_name LIKE '%a%';

# 转义方法一:\ 进行转义
SELECT
last_name
FROM
employees
WHERE
last_name LIKE '_\_%';
# 转移方法二:ESCAPE
SELECT
last_name
FROM
employees
WHERE
last_name LIKE '_$_%' ESCAPE '$';

# between and
# 提高语句简洁度(包含连接值,等同于 >= <=)
SELECT
*
FROM
employees
WHERE
employee_id BETWEEN 100 AND 120;

# in
# in 列表的值类型需要统一或者兼容('123' 和 123)
SELECT
last_name
FROM
employees
WHERE employee_id in ('IT','AD');

# is null
# = 和 <> 不能用来判断 NULL
SELECT
last_name, commission_pct
FROM
employees
WHERE
commission IS NOT NULL;

# 安全等于:<=>
# 安全等于可以判断数值和 NULL
SELECT
last_name, commission_pct
FROM
employees
WHERE
commission <=> NULL;

排序查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
语法:
SELECT
FROM
ORDER BY 【asc(可省略) or desc(降序)】
注意:
order by 可以支持单个字段,多个字段,表达式,函数,别名
order by 一般放在查询语句的最后面,limit 子句除外
*/

# 案例
SELECT * FROM employees ORDER BY salary DESC;
SELECT * FROM employees ORDER BY salary ASC;

SELECT *
FROM employees
WHERE department_id >= 90
ORDER BY hiredate ASC;

# 加入表达式
SELECT *, salary*12*(1+IFNULL(commission_pct, 0)) 年薪
FROM employees
ORDER BY salary*12*(1+IFNULL(commission_pct, 0)) DESC;

# 表达式加别名
SELECT *, salary*12*(1+IFNULL(commission_pct, 0)) 年薪
FROM employees
ORDER BY 年薪 DESC;

# 加入函数
SELECT LENGTH(last_name) 字节长度
FROM employees
ORDER BY LENGTH(last_name) DESC;

# 多个字段排序
FROM *
FROM employees
ORDER BY salary ASC, employee_id DESC;

常见函数

1
2
3
# 功能:将一组逻辑语句封装在方法体中,对外暴露方法名
# 调用:
SELECT 函数名(实参列表) 【FROM 表】;

字符函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# length 获取参数值的字节个数
SELECT LENGTH('john');
# concat 拼接字符串
SELECT CONCAT(last_name,first_name) 姓名 FROM employees;
# upper lower
SELECT CONCAT(upper(last_name), LOWER(first_name)) 姓名 FROM employees;
# substr substring
# 索引从1开始
# 两个参数从指定索引数指定字符长度的字符
SELECT SUBSTR('李莫愁爱上了陆展元',7) out_put;
SELECT SUBSTR('李莫愁爱上了陆展元',1,3) out_put;

# instr 返回子串出现的第一次索引,没有返回 0
SELECT INSTR('杨不悔爱上了殷六侠','殷六侠') AS out_put;

# trim 去掉前后字符
SELECT TRIM(' 张翠山 ') AS out_put;
SELECT TRIM('a' FROM ' 张翠山 ') AS out_put;

# lpad 用指定的字符实现左填充指定长度
SELECT LPAD('殷素素', 10, '*') AS out_put;

# rpad 用指定的字符实现右填充指定长度
SELECT RPAD('殷素素', 10, '*') AS out_put;

# replace 替换
SELECT REPLACE('张无忌爱上了周芷若周芷若','周芷若','赵敏') AS out-put;

数学函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# round 四舍五入
SELECT ROUND(1.55);

# ceil 向上取整 返回大于等于该参数的最小整数
SELECT CEIL(1.00)

# floor 向下取整 返回小于等于该参数的最小整数
SELECT FLOOR(-9.99)

# truncate 截断 保留小数点后几位
SELECT TRUNCATE(1.89, 1)

# mod 取余 mod(a,b) = a - a/b*b
SELECT MOD(10, 3);
SELECT 10/3;

日期函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# now 返回当前系统日期 + 时间
SELECT NOW()

# curdate 返回当前时间,不包含日期
SELECT CURDATE()

# curtime 返回当前时间,不包含日期
SELECT CURTIME()

# 可以获取指定的部分,年 月 日 小时 分钟 秒
SELECT YEAR(NOW()) 年;
SELECT YEAR('2018-1-1') 年;

SELECT YEAR(hiredate) 年 FROM employees;

SELECT MONTH(NOW()) 月;
SELECT MONTHNAME(NOW()) 月;

# str_to_date 将日期格式的字符转换为指定格式的日期
SELECT STR_TO_DATE('9-13-1999','%m-%d-%Y');
SELECT * FROM employees WHERE hiredate = STR_TO_DATE('4-3 1992', '%c-%d %Y');

# date_format 将日期转化成字符
SELECT DATE_FORMAT(NOW(), '%y年%m月%d日') AS out_put;

其他函数

1
2
3
SELECT VERSION();
SELECT DATABASE();
SELECT USER();

流程控制函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# if 函数:if else 效果
SELECT IF(10<5, '大', '小');
SELECT last_name, commission_pct, IF(commission_pct IS NULL, '没奖金,呵呵','有将近,嘻嘻') 备注;

# case 函数作用一:switch case 效果
SELECT salary 原始工资, department_id,
CASE department_id
WHEN 30 THEN salary * 1.1
WHEN 40 THEN salary * 1.2
WHEN 50 THEN salary * 1.3
ELSE salary
END AS 新工资
FROM employees;

# case 函数作用二:多重 if 效果
SELECT salary,
CASE
WHEN salary>20000 THEN 'A'
WHEN salary>15000 THEN 'B'
WHEN salary>10000 THEN 'C'
ELSE 'D'
END AS 评级
FROM employees;

分组函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 用作统计使用,又称为聚合函数或者统计函数或者组函数
# 简单的使用
SELECT SUM(salary) FROM employees;
SELECT AVG(salary) FROM employees;
SELECT MIN(salary) FROM employees;
SELECT MAX(salary) FROM employees;
SELECT COUNT(salary) FROM employees;

SELECT SUM(salary) 和, AVG(salary)平均
FROM employees;

# 参数支持哪些类型
# SUM AVG 数值型
# MIN MAX COUNT 所有类型

# 是否忽略 null
# SUM AVG MIN MAX COUNT 都忽略 null 值

# 可以和 DISTINCT 搭配使用
SELECT COUNT(DISTINCT salary) FROM employees;

# count 函数的详细介绍
# 统计行数
SELECT COUNT(*) FROM employees;
SELECT count(1) FROM employees; # 相当于增加了一列常量值,然后统计常量值个数,相当于统计行数

分组查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
语法:
SELECT 分组函数,列
FROM 表
WHERE 筛选条件
GROUP BY 分组的列表
ORDER BY 子句
注意:
查询列表必须特殊,要求是分组函数和group by之后出现的字段
分组前筛选用 WHERE 数据源是原始表 放在分组前
分组后筛选用 HAVING 数据源是分组后的结果集 放在分组后
支持多个字段分组
可以添加排序,放在最后
*/

# 查询每个工种最高工资
SELECT MAX(salary), job_id
FROM employees
GROUP BY job_id;

# 查询每个位置上的部门个数
SELECT COUNT(*), location_id
FROM departments
GROUP BY location_id;

# 查询每个部门,邮件中有a的员工,的平均工资
SELECT AVG(salary), departemnt_id
FROM employees
WHERE email LIKE '%a%'
GROUP BY department_id;

# 查询哪个部门的员工个数大于2
# 添加分组后的筛选条件使用 HAVING
SELECT COUNT(*), department_id
FROM employees
GROUP BY department_id
HAVING COUNT(*) > 2;

# 按照表达式或函数分组
# 按照员工姓名的长度分组,查询每一组员工的个数,并且筛选员工个数大于五
SELECT COUNT(*) c, LENGTH(last_name) len_name
FROM employees
GROUP BY len_name
HAVING c > 5;

# 按照多个字段分组
SELECT AVG(salary), department_id, job_id
FROM employees
GROUP BY job_id. departemt_id;

# 添加排序
SELECT AVG(salary) a, department_id, job_id
FROM employees
WHERE department_id IS NOT NULL
GROUP BY job_id, department_id
HAVING a > 10000
ORDER BY a DESC;

连接查询

按照年份分类

  • sql92 标准:支持内连接
  • sql99 标准:支持内连接,外连接(左右连接)和交叉连接

按照功能分类

  • 内连接(等值连接,非等值连接,自连接)
  • 外连接(左外连接,右外连接,全外连接)
  • 交叉连接

sql92 标准

等值连接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
用法:
SELECT name, boyName
FROM boys, bueaty
WHERE bueaty.boyfriend_id = boys.id;
*/

# 查询员工名,工种号,工种名
SELECT last_name, employees.job_id, job_title
FROM employees, jobs
WHERE employees.'job_id' = jobs.'job_id';

# 为表起别名
SELECT e.last_name, e.job_id, j.job_title
FROM employees e, jobs j
WHERE e.'job_id' = j.'job_id';

# 加筛选
# 查询有将近的员工名和部门名
SELECT last_name, department_name, commission_pct
FROM employees e, departments d
WHERE e.'department_id' = d.'department_id'
AND e.'commission_pct' IS NOT NULL;

# 加分组
# 查询每个城市的部门数量
SELECT COUNT(*) 个数, city
FROM departments d, locations l
WHERE d.'location_id' = l.'location_id'
GROUP BY city;

# 查询有奖金的部门名和部门领导编号和最低工资
SELECT department_name, manager_id, MIN(salary)
FROM department d, employees e
WHERE d.'department_id' = e.'department_id'
AND commission_pct IS NOT NULL
GROUP BY department_name, d.manager_id;

# 加排序
# 查询每个工种的工种名,员工个数,并且按照员工个数降序
SELECT job_titile, COUNT(*)
FROM employees e, jobs j
WHERE e.'job_id' = j.'job_id'
GROUP BY job_title
ORDER BY COUNT(*) DESC;

# 三表连接
SELECT last_name, department_name,city
FROM emplyees e, departments d, locations l
WHERE e.'departemnt_id' = d.'department_id'
AND d.'location_id' = l.'location_id';

非等值连接

1
2
3
4
# 查询工资和工资级别
SELECT salary, grade_level
FROM emplyees e, job_grades g
WHERE salary BETWEEN g.'lowest_sal' AND g.'highest_sal';

自连接

1
2
3
4
# 查询员工名和上级名称
SELECT e.employee_id, e.last_name, m.employee_id, m.last_name
FROM employees e, employees m
WHERE e.'manager_id' = m.'employee_id';

sql99 标准

1
2
3
4
5
6
7
8
SELECT 查询列表
FROM 表1 别名 【连接类型】
JOIN 表2 别名
ON 连接条件
WHERE 筛选条件
GROUP BY 分组
HAVING 分组后筛选
ORDER BY 排序列表

内连接 — 等值连接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
语法:
SELECT 查询列表
FROM 表1 别名
INNER JOIN 表2 别名
ON 连接条件
*/


# 查询员工名和部门名
SELECT last_name. department_name
FROM employees, e
INNER JOIN departments d
ON e.'department_id' = d.'department_id';

# 加入筛选
SELECT last_name, job_title
FROM omployees e
INNER JOIN jobs j
ON e.'job_id' = j.'job_id'
WHERE e.'last_name' LIKE '%e%';

# 添加分组加筛选
SELECT city, COUNT(*) 部门个数
FROM departments d
INNER JOIN locations
ON d.'location_id' = l.'location_id'
GROUP BY city
HAVING COUNT(*) > 3;

# 添加排序
SELECT COUNT(*), department_name
FROM employees, e
INNER JOIN departments d
ON e.'department_id' = d.'department_id'
GROUP BY department_id
HAVING COUNT(*) > 3
ORDER BY COUNT(*) DESC;

# 三表连接
SELECT last_name, department)name, job_title
FROM employees e
INNER JOIN departments d
ON e.'department_id' = d.'department_id'
INNER JOIN jobs j
ON e.'job_id' = j.'job_id'
ORDER BY department_name DESC;

内连接 — 非等值连接

1
2
3
4
5
# 查询员工工资级别
SELECT salary, grade_level
FROM employees e
JOIN job_grades g
ON e.'salary' BETWEEN g.'lowest_sal' AND g.'highest_sal';

内连接 — 自连接

1
2
3
4
5
# 查询员工名字和上级的名字
SELECT e.last_nme, m.last_name
FROM employees e
JOIN employees m
ON e.'manager_id' = m.'employee_id';

外连接

应用场景:用于查询一个表中有,另一个表中没有的情况

  • 有主从表之分
  • 外连接的查询结果为主表中的所有记录
  • 左外连接,left join 左边是主表
  • 右外连接,right join 右边是主表
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 查询男朋友不在男神表的女神名(左外连接)
SELECT b.name, bo.*
FROM beauty b
LEFT OUTER JOIN boys bo
ON b.'boyfriend_id' = bo.'id';

# 查询男朋友不在男神表的女神名(右外连接)
SELECT b.name, bo.*
FROM boys bo
RIGHT OUTER JOIN beauty b
ON b.'boyfriend_id' = bo.'id';

# 查询哪个部门没有员工
SELECT d.*, e.employee_id
FROM departments d
LEFT OUTER JOIN employees e
ON d.'department_id' = e.'department_id'
WHERE e.'employee_id' IS NULL;

交叉连接

1
2
3
4
# 标准语法实现笛卡尔乘积
SELECT b.*, bo.*
FROM beauty b
CROSS JOIN boys bo;

连接总结

Here are the different types of the JOINs in SQL:

  • (INNER) JOIN: Returns records that have matching values in both tables

    (INNER) JOIN

  • LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records from the right table

    LEFT (OUTER) JOIN

  • RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched records from the left table

    RIGHT (OUTER) JOIN

  • FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table

    FULL (OUTER) JOIN


子查询

  • 出现在其他语句中的 select 语句,称为子查询或者内查询
  • 外部的查询语句,称为主查询或者外查询
  • 分类:按照子查询出现的位置
    1. select 后面(支持标量子查询)
    2. from 后面(支持表子查询)
    3. where 或者 having 后面(支持标量子查询,行子查询,列子查询)
    4. exists 后面(相关子查询,支持表子查询)
  • 分类:按照结果集的行列数
    1. 标量子查询(结果集只有一行一列)
    2. 列子查询(结果集只有一列多行)
    3. 行子查询(结果集只有一行多列)
    4. 表子查询(结果集一般为多行多列)

where 或者 having 后面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
用法:
where 或者 having 后面
子查询放在小括号内,放在条件的右侧
子查询的执行是优先于主查询执行
注意:
标量子查询搭配着单行的操作符:> < = >= <= <>
列查询一般搭配着多行操作符:in any some all
*/

# 标量子查询(where 后面):谁的工资比 Abel 高
SELECT *
FROM employees
WHERE salary > (
SELECT salary
FROM employees
WHERE last_name = 'Abel'
);

# 标量子查询(having 后面):查询最低工资大于 50 号部门,最低工资的部门id和其最低工资
SELECT MIN(salary), department_id
FROM employees
GROUP BY department_id
HAVING MIN(salary) > (
SELECT MIN(salary)
FROM employees
WHERE department_id = 50
);

# 列子查询(多行子查询):返回 location_id 是 1400 或 1700 之间的部门的所有员工姓名
SELECT last_name
FROM employeesd
WHERE department_id IN(
SELECT DISTINCT department_id
FROM departments
WHERE location_id IN (1400, 1700)
);

# 行子查询(结果集一行多列或者多行多列)
# 案例:查询员工编号最小并且工资最高的员工信息
SELECT *
FROM employees
WHERE (employee_id, salary) = (
SELECT MIN(emplyee_id), MAX(salary)
FROM employees
);

select 后面

1
2
3
4
5
6
7
# 查询每个部门的员工个数
SELECT d.*, (
SELECT COUNT(*)
FROM employees
WHERE e.'department_id' = d.'department_id'
)
FROM departments d;

from 后面

1
2
3
4
5
6
7
8
9
10
11
12
13
/*
用法:将子查询的结果充当一张表,要求必须起别名
*/

# 查询每个部门的平均工资的工资等级
SELECT ag_dep.*, g.'level'
FROM (
SELECT AVG(salary) ag, department_id
FROM employees
GROUP BY department_id
) ag_dep
INNER JOIN job_grades g
ON ag_dep.ag BETWEEN lowest_sal AND highest_sal

exists 后面(相关子查询)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
语法:
查询结果有没有,返回布尔类型
示例:
SELECT EXISTS(
SELECT employee_id
FROM employees
WHERE salary = 30000
);
*/


# 查询有员工的部门名
SELECT department_name
FROM departments d
WHERE EXISTS(
SELECT *
FROM employees e
WHERE d.'department_id' = e.'department_id'
);

# 查询没有女朋友的男神信息
# 使用 IN 的方法
SELECT bo.*
FROM boys bo
WHERE bo.id NOT IN(
SELECT boyfriend_id
FROM bueaty
)

# 使用 EXISTS 的方法
SELECT bo.*
FROM boys bo
WHERE NOT EXISTS(
SELECT boyfriend_id
FROM beauty b
WHERE bo.'id' = b.'boyfriend_id'
);

分页查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
使用场景:
当显示的数据,一页显示不全,需要分页提交 sql 请求
语法:
SELECT 查询列表
FROM 表
LIMIT offset, size;

offset: 要显示的条目的起始索引
size:要显示的条目个数
limit 放在查询语句的最后,执行顺序也是最后
*/

# 查询前五条员工信息
SELECT *
FROM employees
LIMIT 0,5;

# 查询第十一条到第二十五条
SELECT *
FROM employees
LIMIT 10, 15;

# 查询有奖金的员工中工资较高的前十名
SELECT *
FROM employees
WHERE commission_pct IS NOT NULL
ORDER BY salary DESC
LIMIT 10;

union 联合查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
作用:将多条查询语句的结果合并成一个结果
语法:
查询语句 1
UNION
查询语句 2
UNION
...
*/

# 查询部门编号大于 90 或者邮箱包含 a 的员工信息
SELECT * FROM employees WHERE email LIKE '%a%' OR department_id > 90;
# 或者
SELECT * FROM employees WHERE email LIKE '%a%'
UNION
SELECT * FROM employees WHERE department_id > 90;
Author

Haojun(Vincent) Gao

Posted on

2020-08-17

Updated on

2022-02-22

Licensed under

Comments