Oracle

1、条件限制和排序

条件限制的关键字:WHERE

  1. SELECT employee_id, last_name, job_id, department_id
  2. FROM employees
  3. WHERE department_id = 90 ;

image.png

2、比较操作符

符号 意义
= 等于
> 大于
>= 大于等于
< 小于
<= 小于等于
<> 不等于
BETWEEN …AND… 在两个值之间
IN(set) 在一个集合范围内
LIKE 匹配一个字符串样子,可以使用%通配符
IS NULL 是一个空值,注意不能使用 =NULL
  1. SELECT last_name, salary
  2. FROM employees
  3. WHERE salary <= 6000;

image.png

  1. SELECT last_name, salary
  2. FROM employees
  3. WHERE salary BETWEEN 2500 AND 3500;

image.png

  1. SELECT employee_id, last_name, salary, manager_id
  2. FROM employees
  3. WHERE manager_id IN (100, 101, 201);

image.png

3、使用LIKE做模糊匹配

可使用**%** 或者**_** 作为通配符:
**%** 代表 0个或者多个 字符.
**_** 代表一个单个字符.

  1. SELECT last_name
  2. FROM employees
  3. WHERE last_name LIKE '_o%';

image.png

4、要搜索统配符本身-使用ESCAPE 标识转义字符

  1. select * from t_char where a like '%\%%' escape '\';

5、使用NULL条件查询

  1. SELECT last_name, manager_id
  2. FROM employees
  3. WHERE manager_id IS NULL;

image.png

6、多条件组合的逻辑操作符

逻辑操作符 意义
AND 所有条件都满足,返回TRUE
OR 只要有一个条件满足,返回TRUE
NOT 如果条件是FALSE,返回TRUE
  1. SELECT employee_id, last_name, job_id, salary
  2. FROM employees
  3. WHERE salary >=2000
  4. AND job_id LIKE '%MAN%';

image.png

  1. SELECT employee_id, last_name, job_id, salary
  2. FROM employees
  3. WHERE salary >= 10000
  4. OR job_id LIKE '%MAN%';

image.png

  1. SELECT last_name, job_id
  2. FROM employees
  3. WHERE job_id
  4. NOT IN ('IT_PROG', 'ST_CLERK', 'SA_REP');

image.png

7、使用ORDER BY 子句进行排序

  1. SELECT last_name, job_id, department_id, hire_date
  2. FROM employees
  3. ORDER BY hire_date ;

image.png

ASC:升序

DESC:倒序

  1. SELECT last_name, job_id, department_id, hire_date
  2. FROM employees
  3. ORDER BY hire_date DESC;

image.png

按照字段别名排序

  1. SELECT employee_id, last_name, salary*12 annsal
  2. FROM employees
  3. ORDER BY annsal;

image.png

按照多个字段排序

  1. SELECT last_name, department_id, salary
  2. FROM employees
  3. ORDER BY department_id, salary DESC;

image.png

8、操作实例

1、员工信息按先后入职日期排序

  1. select * from emp order by hiredate

2、员工信息按薪水从大到小排序

  1. select * from emp order by sal desc

3、员工信息按部门号和薪水排序

  1. select * from emp order by deptno,sal

按照顺序排序deptno,sal

4、员工信息按部门和薪水排列,降序

  1. select * from emp order by deptno desc,sal desc

【结论】:asc|desc 作用在前面那列

5、员工信息按奖金倒序

  1. select * from emp order by comm desc

NULL 会影响排序

  1. select * from emp order by comm desc nulls last

6、员工信息按第2列排序

  1. select * from emp order by 2

【 结论】:按照返回结果集第几列来排序,而不是表的第几列

7、员工信息按别名排序

  1. select enmno ,sal "月薪" from emp order by "月薪";

可以根据别名进行排序