WHERE子句
- 使用WHERE 子句,将不满足条件的行过滤掉。
示例:
SELECT employee_id, last_name, job_id, department_id FROM employees WHERE department_id = 90 ;
字符和日期
- 字符和日期要包含在单引号中。
- 字符大小写敏感,日期格式敏感。
示例:
SELECT last_name, job_id, department_id FROM employees WHERE last_name = ‘Whalen‘;
比较运算
操作符 | 含义 |
= | 等于 |
> | 大于 |
>= | 大于等于 |
< | 小于 |
<= | 小于等于 |
<> | 不等于(也可以是!=) |
赋值使用 := 符号
比较运算示例:
SELECT last_name, salary FROM employees WHERE salary <= 3000;
其它比较运算
操作符 | 含义 |
BETWEEN...AND... | 在两个值之间 (包含边界) |
IN(set) | 等于值列表中的一个 |
LIKE | 模糊查询 |
IS NULL | 空值 |
使用 BETWEEN 运算来显示在一个区间内的值
示例一:
SELECT last_name, salary FROM employees WHERE salary BETWEEN 2500 AND 3500;
使用 IN运算显示列表中的值
SELECT employee_id, last_name, salary, manager_id FROM employees WHERE manager_id IN ( 100, 101, 201 );
LIKE
- 使用 LIKE 运算选择类似的值
- 选择条件可以包含字符或数字:
- % 代表零个或多个字符(任意个字符)。
- _ 代表一个字符。
示例:
SELECT first_name FROM employees WHERE first_name LIKE ‘S%‘;
‘%’和‘-’同时使用
示例:
SELECT last_name FROM employees WHERE last_name LIKE ‘_o%‘;
ESCAPE
- 可以使用 ESCAPE 标识符 选择‘%’和 ‘_’ 符号。
- 回避特殊符号的:使用转义符。例如:将[%]转为[\%]、[_]转为[\_],然后再加上[ESCAPE ‘\’] 即可。
示例:
SELECT job_id FROM jobs WHERE job_id LIKE ‘IT\_%‘ ESCAPE ‘\‘;
NULL
- 使用 IS (NOT) NULL 判断空值。
SELECT last_name, manager_id FROM employees WHERE manager_id IS NULL;
逻辑运算
操作符 | 含义 |
AND | 逻辑并 |
OR | 逻辑或 |
NOT | 逻辑否 |
AND
- AND 要求并的关系为真。
示例:
SELECT employee_id, last_name, job_id, salary FROM employees WHERE salary >= 10000 AND job_id LIKE ‘%MAN%‘;
OR
- OR 要求或关系为真。
SELECT employee_id, last_name, job_id, salary FROM employees WHERE salary >= 10000 OR job_id LIKE ‘%MAN%‘;
NOT
- NOT 不在这之内
SELECT last_name, job_id FROM employees WHERE job_id NOT IN ( ‘IT_PROG‘, ‘ST_CLERK‘, ‘SA_REP‘ );
优先级
优先级 | 名称 |
1 | 算术运算符 |
2 | 连接符 |
3 | 比较符 |
4 | IS [NOT] NULL, LIKE, [NOT] IN |
5 | [NOT] BETWEEN |
6 | NOT |
7 | AND |
8 | OR |
- 可以使用括号改变优先级顺序
ORDER BY子句
- 使用 ORDER BY 子句排序
- ASC(ascend): 升序
- DESC(descend): 降序
- ORDER BY 子句在SELECT语句的结尾。
示例:
SELECT last_name, job_id, department_id, hire_date FROM employees ORDER BY hire_date;
降序排列
SELECT last_name, job_id, department_id, hire_date FROM employees ORDER BY hire_date DESC;
按别名排序:
SELECT employee_id, last_name, salary * 12 annsal FROM employees ORDER BY annsal;
多个列排序:按照ORDER BY 列表的顺序排序。
SELECT last_name, department_id, salary FROM employees ORDER BY department_id, salary DESC;
原文地址:https://www.cnblogs.com/loaderman/p/11731650.html
时间: 2024-10-11 21:32:22