进阶2:条件查询
/*
语法:
select
查询列表
from
表名
where
筛选条件;
分类:
一、按条件表达式筛选
条件运算符: < > = != <> >= <=
**二、按逻辑运算符筛选**
逻辑运算符: && || ! and or not
**三、模糊查询**
like
between and
in
is null
*/
#1.按条件表达式筛选
#案例1:查询员工工资大于12000的
SELECT
*
FROM
employees
WHERE
salary>12000;
#案例2:查询部门编号不等于90号的员工名和部门编号
SELECT
last_name,
department_id
FROM
employees
WHERE
department_id!=90;
#二、按逻辑运算符筛选
#案例1:查询员工工资在10000到20000之间的员工名、工资及奖金
SELECT
last_name,
salary,
commission_pct
FROM
employees
WHERE
salary>10000 AND salary<20000;
#案例2:查询部门编号不是在90到110之间,或者工资大于15000的员工信息
SELECT
*
FROM
employees
WHERE
NOT(department_id>=90 AND department_id<=110)OR salary>15000;
#department_id<90 or department_id>110 or salary>15000;
#三、模糊查询
/*
like
between and
in
is null | is not null
*/
#1.like
/*特点:
① 一般和通配符搭配使用
通配符:
% 任意多个字符,包含0个字符
_ 任意单个字符
*/
#案例1:查询员工名中包含字符型a的员工信息
SELECT
*
FROM
employees
WHERE
last_name LIKE '%a%';
#案例2:查询员工名中第三个字符为n,第五个字符为l的员工名和工资
SELECT
last_name,
salary
FROM
employees
WHERE
last_name LIKE '__n_l%';
#案例3:查询员工名中第二个字符为_的员工名和工资
SELECT
last_name,
salary
FROM
employees
WHERE
#last_name LIKE '_\_%';
last_name LIKE '_$_%' ESCAPE '$';
#2.between and
/
①使用between and 可以提高语句简洁度
②包含临界值
③这两个临界值不可以调换位置
/
#案例1:查询员工编号在100到120之间的员工信息
SELECT
*
FROM
employees
WHERE
employee_id>=100 AND employee_id<=120;
#------------------------------------------------------------#
SELECT
*
FROM
employees
WHERE
employee_id BETWEEN 100 AND 120;
#3.in
/*
含义:判断某字段的值是否属于in列表中的一项
特点:
①使用in提高语句简洁度
②in列表的的值类型必须一致或者兼容
③不支持通配符
*/
#案例:查询员工的工种编号是IT_PROG、AD_VP、AD_PRES中的一个员工名和工种编号
SELECT
last_name,
job_id
FROM
employees
WHERE
job_id ='IT_PROG' OR job_id ='AD_PRES'OR job_id ='AD_VP' ;
#--------------------------------------------------------------------#
SELECT
last_name,
job_id
FROM
employees
WHERE
job_id IN ('IT_PROG' ,'AD_PRES','AD_VP') ;
#4.is not
/*
=或<>不能判断not 值
*/
#案例1:查询没有奖金的员工名和奖金率
SELECT
last_name,
commission_pct
FROM
employees
WHERE
commission_pct IS NULL;
#案例2:查询有奖金的员工名和奖金率
SELECT
last_name,
commission_pct
FROM
employees
WHERE
commission_pct IS NOT NULL;
#-------------------------以下为错误-------------------------------------#
SELECT
last_name,
commission_pct
FROM
employees
WHERE
salary IS 12000;
#5.安全等于 <=>
#案例1:查询没有奖金的员工名和奖金率
SELECT
last_name,
commission_pct
FROM
employees
WHERE
commission_pct <=> NULL;
#案例2:查询工资为12000的员工信息
SELECT
last_name,
salary
FROM
employees
WHERE
salary <=> 12000;
#6.安全等于 <=> 与is null 比较
IS NULL :仅仅可以判断 NULL 值,可读性较高
<=>:既可以判断 NULL 值 也可以判断 普通的数值,可读性较低