MYSQL初阶学习笔记——进阶2:条件查询
#进阶2:条件查询
/*
语法:
select
查询列表
from
表名
where
筛选条件;
分类:
一、按条件表达式筛选
简单条件运算符:> < = !=或<> >= <=
二、按逻辑表达式筛选
逻辑运算符:
&& || !
and or not
三、模糊查询
like
between and
in
is null
*/
#一、按条件表达式筛选
#案例一:查询工资>12000的员工信息
SELECT
*
FROM
employees
WHERE salary > 12000 ;
#案例二:查询部门编号不等于90号的员工名和部门编号
SELECT
first_name,
last_name,
department_id
FROM
employees
WHERE department_id <> 90 ;
#二、按逻辑表达式筛选
#案例一:查询工资在10000到20000之间的员工名、工资以及奖金
SELECT
last_name,
salary,
commission_pct
FROM
employees
WHERE
salary>=10000 AND salary<=20000;
#案例二:查询部门编号不是在90~110之间,或者工资高于15000的员工信息
SELECT
*
FROM
employees
WHERE
NOT(department_id>=90 AND department_id<=110) OR salary>15000
#案例三:模糊查询
/*
like
通配符:%代表多个字符串;_代表一个字符
between and
in
is null | is not null
*/
#1.like 包含xxx的字符串
##案例一:查询员工名中包含字符a的员工信息
SELECT
*
FROM
employees
WHERE
last_name LIKE '%a%'; #必须用单引号''
##案例二:查询员工名中第三个字符为u,第五个字符为l的员工名和工资
SELECT
last_name,
salary
FROM
employees
WHERE
last_name LIKE '__u_l%';
##案例三:查询员工名中第二个字符为_的员工信息
SELECT
*
FROM
employees
WHERE
last_name LIKE '_\_%'; #\代表转义符号表明第二个_为字符,非通配符
###或者转移符自定义
SELECT
*
FROM
employees
WHERE
last_name LIKE '_$_%' ESCAPE '$'; #使用escape 定义转移符
#2.between and 在xxx~xxx之间
##案例一:查询员工编号在100~120之间的员工信息
SELECT
*
FROM
employees
WHERE
employee_id>=100 AND employee_id<=120;
#=======================================================
SELECT
*
FROM
employees
WHERE
employee_id BETWEEN 100 AND 120;#等价>=100,<=120;数字不可换位置
#3.in 判断某字段的值是否属于 in列表 中的某一项
/*
in列表中类型必须统一或兼容
且不可用通配符表示其内元素
in列表内字符串必须用引号,类似于python列表
*/
##案例一:查询员工的工种编号是IT_PROG、AD_VP、AD_PRES中的一个员工名和工种编号
SELECT
last_name,
job_id
FROM
employees
WHERE
job_id IN('IT_PROG','AD_VP','AD_PRES');
#4.is null
/*
=或<>不能判断null
因此引入 is null 和is not null
*/
##案例:查询奖金为null的员工名和奖金率
SELECT
last_name,
commission_pct
FROM
employees
WHERE
commission_pct IS NULL;
#扩展:安全等于<=> :也可以判断null值,还可以判断普通数值
##例子1
SELECT
last_name,
commission_pct
FROM
employees
WHERE
commission_pct <=> NULL;
##例子2
SELECT
last_name,
salary
FROM
employees
WHERE
salary <=> 11000;
#======================================
#test1
#1.查询工资大于12000的员工姓名和工资
SELECT
last_name,
first_name,
salary
FROM
employees
WHERE
salary>12000;
#2.查询员工号为176的员工姓名和部门号和年薪
SELECT
last_name,
first_name,
department_id,
salary*12*(1+IFNULL(commission_pct,0)) AS 年薪
FROM
employees
WHERE
employee_id=176;
#3.选择工资不在5000~12000的员工姓名和工资
SELECT
last_name,
first_name,
salary
FROM
employees
WHERE
salary>=12000 OR salary<=5000;
#4.选择在20或50号部门工作的员工姓名和部门号
SELECT
last_name,
first_name,
department_id
FROM
employees
WHERE
department_id IN (20,50);
#5.选择公司中没有管理者的员工姓名和job_id
SELECT
last_name,
first_name,
job_id,
manager_id
FROM
employees
WHERE
manager_id IS NULL;
#6.选择公司中有奖金的员工姓名,工资和奖金级别
SELECT
last_name,
first_name,
salary,
commission_pct
FROM
employees
WHERE
commission_pct IS NOT NULL;
#7.选择员工last_name的第三个字母是a的员工姓名
SELECT
last_name,
first_name
FROM
employees
WHERE
last_name LIKE '__a%';
#8.选择last_name中有字母a和e的员工姓名
SELECT
last_name,
first_name
FROM
employees
WHERE
last_name LIKE '%a%' OR last_name LIKE '%e%' ;
#9.显示出表employees表中first_name以'e'结尾的员工信息
SELECT
last_name,
first_name
FROM
employees
WHERE
first_name LIKE '%e';
#10.显示出employees部门编号在80~100之间的姓名,职位
SELECT
last_name,
first_name,
job_id,
department_id
FROM
employees
WHERE
department_id BETWEEN 80 AND 100;
#11.显示出表employees的manager_id是100,101,102的员工姓名、职位
SELECT
last_name,
first_name,
manager_id
FROM
employees
WHERE
manager_id IN(100,101,102);
#=======================综合测试=========================
#1.查询没有奖金,且工资小于18000的salary和last_name
SELECT
salary,
last_name
FROM
employees
WHERE
commission_pct IS NULL AND salary<18000;
#2.查询employees表中,job_id不为’IT‘或者工资为12000的员工信息
SELECT
*
FROM
employees
WHERE
job_id <>'IT' OR salary=12000;
#3.查看部门departments表的结构 desc使用
DESC departments;
#4.查询部门departments表中涉及哪些位置编号
SELECT DISTINCT
location_id
FROM
departments;
/*
语法:
select
查询列表
from
表名
where
筛选条件;
分类:
一、按条件表达式筛选
简单条件运算符:> < = !=或<> >= <=
二、按逻辑表达式筛选
逻辑运算符:
&& || !
and or not
三、模糊查询
like
between and
in
is null
*/
#一、按条件表达式筛选
#案例一:查询工资>12000的员工信息
SELECT
*
FROM
employees
WHERE salary > 12000 ;
#案例二:查询部门编号不等于90号的员工名和部门编号
SELECT
first_name,
last_name,
department_id
FROM
employees
WHERE department_id <> 90 ;
#二、按逻辑表达式筛选
#案例一:查询工资在10000到20000之间的员工名、工资以及奖金
SELECT
last_name,
salary,
commission_pct
FROM
employees
WHERE
salary>=10000 AND salary<=20000;
#案例二:查询部门编号不是在90~110之间,或者工资高于15000的员工信息
SELECT
*
FROM
employees
WHERE
NOT(department_id>=90 AND department_id<=110) OR salary>15000
#案例三:模糊查询
/*
like
通配符:%代表多个字符串;_代表一个字符
between and
in
is null | is not null
*/
#1.like 包含xxx的字符串
##案例一:查询员工名中包含字符a的员工信息
SELECT
*
FROM
employees
WHERE
last_name LIKE '%a%'; #必须用单引号''
##案例二:查询员工名中第三个字符为u,第五个字符为l的员工名和工资
SELECT
last_name,
salary
FROM
employees
WHERE
last_name LIKE '__u_l%';
##案例三:查询员工名中第二个字符为_的员工信息
SELECT
*
FROM
employees
WHERE
last_name LIKE '_\_%'; #\代表转义符号表明第二个_为字符,非通配符
###或者转移符自定义
SELECT
*
FROM
employees
WHERE
last_name LIKE '_$_%' ESCAPE '$'; #使用escape 定义转移符
#2.between and 在xxx~xxx之间
##案例一:查询员工编号在100~120之间的员工信息
SELECT
*
FROM
employees
WHERE
employee_id>=100 AND employee_id<=120;
#=======================================================
SELECT
*
FROM
employees
WHERE
employee_id BETWEEN 100 AND 120;#等价>=100,<=120;数字不可换位置
#3.in 判断某字段的值是否属于 in列表 中的某一项
/*
in列表中类型必须统一或兼容
且不可用通配符表示其内元素
in列表内字符串必须用引号,类似于python列表
*/
##案例一:查询员工的工种编号是IT_PROG、AD_VP、AD_PRES中的一个员工名和工种编号
SELECT
last_name,
job_id
FROM
employees
WHERE
job_id IN('IT_PROG','AD_VP','AD_PRES');
#4.is null
/*
=或<>不能判断null
因此引入 is null 和is not null
*/
##案例:查询奖金为null的员工名和奖金率
SELECT
last_name,
commission_pct
FROM
employees
WHERE
commission_pct IS NULL;
#扩展:安全等于<=> :也可以判断null值,还可以判断普通数值
##例子1
SELECT
last_name,
commission_pct
FROM
employees
WHERE
commission_pct <=> NULL;
##例子2
SELECT
last_name,
salary
FROM
employees
WHERE
salary <=> 11000;
#======================================
#test1
#1.查询工资大于12000的员工姓名和工资
SELECT
last_name,
first_name,
salary
FROM
employees
WHERE
salary>12000;
#2.查询员工号为176的员工姓名和部门号和年薪
SELECT
last_name,
first_name,
department_id,
salary*12*(1+IFNULL(commission_pct,0)) AS 年薪
FROM
employees
WHERE
employee_id=176;
#3.选择工资不在5000~12000的员工姓名和工资
SELECT
last_name,
first_name,
salary
FROM
employees
WHERE
salary>=12000 OR salary<=5000;
#4.选择在20或50号部门工作的员工姓名和部门号
SELECT
last_name,
first_name,
department_id
FROM
employees
WHERE
department_id IN (20,50);
#5.选择公司中没有管理者的员工姓名和job_id
SELECT
last_name,
first_name,
job_id,
manager_id
FROM
employees
WHERE
manager_id IS NULL;
#6.选择公司中有奖金的员工姓名,工资和奖金级别
SELECT
last_name,
first_name,
salary,
commission_pct
FROM
employees
WHERE
commission_pct IS NOT NULL;
#7.选择员工last_name的第三个字母是a的员工姓名
SELECT
last_name,
first_name
FROM
employees
WHERE
last_name LIKE '__a%';
#8.选择last_name中有字母a和e的员工姓名
SELECT
last_name,
first_name
FROM
employees
WHERE
last_name LIKE '%a%' OR last_name LIKE '%e%' ;
#9.显示出表employees表中first_name以'e'结尾的员工信息
SELECT
last_name,
first_name
FROM
employees
WHERE
first_name LIKE '%e';
#10.显示出employees部门编号在80~100之间的姓名,职位
SELECT
last_name,
first_name,
job_id,
department_id
FROM
employees
WHERE
department_id BETWEEN 80 AND 100;
#11.显示出表employees的manager_id是100,101,102的员工姓名、职位
SELECT
last_name,
first_name,
manager_id
FROM
employees
WHERE
manager_id IN(100,101,102);
#=======================综合测试=========================
#1.查询没有奖金,且工资小于18000的salary和last_name
SELECT
salary,
last_name
FROM
employees
WHERE
commission_pct IS NULL AND salary<18000;
#2.查询employees表中,job_id不为’IT‘或者工资为12000的员工信息
SELECT
*
FROM
employees
WHERE
job_id <>'IT' OR salary=12000;
#3.查看部门departments表的结构 desc使用
DESC departments;
#4.查询部门departments表中涉及哪些位置编号
SELECT DISTINCT
location_id
FROM
departments;