先查询有无这个表,再去看筛选条件再进行查询 (from-->where-->select)
# 进阶2 :条件查询
/*
语法:
select
查询列表
from
表名
where
筛选条件;
分类:
一、按条件表达式筛选
条件运算符: >, < , =, !=, <>, >=, <=
二、按逻辑表达式筛选
逻辑运算符:
作用:用于连接条件表达式
&& || !
and or not
三、模糊查询
like
between
in
is null
*/
#一、按条件表达式筛选
#案例1: 查询工资>12000的员工信息
SELECT * FROM employees WHERE salary > 12000;
#案例2: 查询部门编号不等于90号的员工名和部门编号
SELECT
CONCAT(first_name," ",last_name) AS "姓名" ,
department_id AS "部门编号"
FROM
employees
WHERE
department_id <> 90;
#二、按逻辑表达式筛选
#案例1: 查询工资在10000到20000之间的员工名、工资以及奖金
SELECT
CONCAT(`first_name`," ",`last_name`) AS "姓名",
salary,
commission_pct
FROM
employees
WHERE
salary>=10000 && salary<=20000;
#案例2:查询部门编号不是在90到110之间,或者工资高于15000的员工信息
# 方法一:
SELECT
*
FROM
employees
WHERE
department_id < 90 OR department_id > 110 OR salary >15000;
# 方法二:
SELECT
*
FROM
employees
WHERE
NOT (department_id >= 90 AND department_id <= 110 ) OR salary >15000;
#三、模糊查询
/*
like
between and
in
is null || is not
*/
#---------------------------------------------------------------
#1. like
/*
特点:
①一般和通配符搭配使用
通配符:
%:任意多个字符(包含0个字符)
_: 任意单个字符
*/
# 案例1:查询员工名中包含字符a的员工信息
SELECT
*
FROM
employees
WHERE
last_name LIKE ‘%a%‘; # abc 也可以查询到
# 案例2:查询员工名中第三个字符为e,第五个字符为a的员工名和工资
SELECT
last_name,
salary
FROM
employees
WHERE
last_name LIKE ‘__n_l%‘; #Hunold
#案例3: 查询员工名中第二个字符为_的员工名
# 方法一: 转义 \_
SELECT
last_name
FROM
employees
WHERE
last_name LIKE ‘_\_%‘;
#方法二:转义escape ( $_ ESCAPE ‘$‘)
SELECT
last_name
FROM
employees
WHERE
last_name LIKE ‘_$_%‘ ESCAPE ‘$‘;
#---------------------------------------------------------------
#2. between and
/*
①使用between and 可以提高语句的简洁度
②包含临界值
③两个临界值不要调换顺序
*/
#案例1: 查询员工编号在100到120之间的员工信息
SELECT
*
FROM
employees
WHERE
employee_id BETWEEN 100 AND 120;
#---------------------------------------------------------------
# 3. in
/*
含义:判断某字段的值是否属于in列表中的某一项
特点:
①使用in提高语句简洁度
②in列表的值类型必须一致或兼容(‘123‘ vs 123)
*/
#案例1: 查询员工的工种编号是IT_PROT、AD_PRES、AD_VP中的一个员工名和工种编号
SELECT
last_name,
job_id
FROM
employees
WHERE
job_id IN (‘IT_PROT‘,‘AD_PRES‘ , ‘AD_VP‘)
#---------------------------------------------------------------
#4.is null
/*
=或<>不能用于判读null值
is null 或is not null能判断null值
*/
#案例1: 查询没有奖金的员工名和奖金率
SELECT
last_name,
commission_pct
FROM
employees
WHERE
commission_pct IS NOT NULL;
#---------------------------------------------------------------
# 安全等于 <=> 可读性比较差
#案例1: 查询没有奖金的员工名和奖金率
SELECT
last_name,
commission_pct
FROM
employees
WHERE
commission_pct <=> NULL;
#案例2: 查询工资为12000的员工信息
SELECT
last_name,
salary
FROM
employees
WHERE
salary <=> 12000;
# is null vs <=>
/*
IS NULL : 仅可以判断NULL值,可读性较高,建议使用
<=> : 既可以判断NULL值,又可以判断普通的数据,可读性较低
*/
练习
# 案例一: 查询没有奖金,且工资小于18000的salary,last_name
SELECT
salary ,
last_name
FROM
employees
WHERE
commission_pct IS NULL AND salary < 18000;
# 案例二: 查询employees表中,job_id不为‘IT’或者工资为12000的员工信息
SELECT
*
FROM
employees
WHERE
job_id <> ‘IT‘ OR salary =12000;
# 案例三: 查看部门department表的结构
DESC departments;
#案例四:查询部门departments表中涉及到了哪些位置编号
SELECT
DISTINCT location_id
FROM
departments;
# 案例五---经典面试题
/*
试问 :
select * from employees;
和
select * from employees where commission_pct like ‘%%‘ and last_name like ‘%%‘ # 不一样
select * from employees where commission_pct like ‘%%‘ or last_name like ‘%%‘ # 一样
结果是否一致? 并说明理由
*/
# 回答
/*
不一样!
因为commission_pct 中 有NULL值
*/