MySQL(2.条件查询)

#二.条件查询
/*
	1.语法
	select 查询列表 from 表名 where 筛选条件;
	
	2.执行顺序
  from  ->  where -> select
*/

#3.案例
-- 一、按关系表达式筛选  关系运算符:>  <  >=  <=  =  <>   补充:也可以使用!=,但不建议
#案例1:查询部门编号不是100的员工信息
select employee_id , first_name , salary , department_id
from employees
where department_id <> 100;

#案例2:查询工资<15000的姓名、工资
select first_name , salary
from employees
where salary < 15000;

-- 二、按逻辑表达式筛选 逻辑运算符:and  or  not   补充:也可以使用&&  ||   !  ,但不建议
#案例1:查询部门编号不是 50-100之间员工姓名、部门编号、邮箱
select first_name , department_id , email
from employees
where department_id<50 or department_id>100;

#案例2:查询奖金率>0.03 或者 员工编号在60-110之间的员工信息
select employee_id , first_name , department_id , commission_pct
from employees
where (commission_pct>0.03) or (employee_id>=60 and employee_id<=110);

-- 三、模糊查询 like/not like 
-- %任意多个模糊匹配  _任意一个模糊匹配,一个下划线占一个位置
#案例1:查询姓名中包含字符a的员工信息
select first_name , salary from employees where first_name like '%a%';
#案例2:查询姓名中包含最后一个字符为e的员工信息
select first_name , salary from employees where first_name like '%e'; 
#案例3:查询姓名中包含第一个字符为e的员工信息
select first_name , salary from employees where first_name like 'e%'; 
#案例4:查询姓名中包含第三个字符为x的员工信息
select last_name , salary from employees where last_name like '__x%';
#案例5:查询姓名中包含第二个字符为_的员工信息
-- \斜杠可以转义后面的_下划线,代表此处_就是一个普通的下划线
select last_name , salary from employees where last_name like '_\_%';
-- escape 后面的字符,表示为此sql中使用的转义符号,代表此处_就是一个普通的下划线
select last_name , salary from employees where last_name like '_#_%' escape '#';

-- 四、查询某字段的值是否属于指定的列表之内  in/not in
#案例1:查询部门编号是30/50/90的员工名、部门编号
select first_name , department_id
from employees
where department_id in (30,50,90);

#案例2:查询工种编号不是SH_CLERK或IT_PROG的员工信息
select * from employees where job_id<>'SH_CLERK' and job_id<>'IT_PROG';
select * from employees where job_id not in ('SH_CLERK','IT_PROG');

-- 五、判断某个字段的值是否介于指定的区间范围  between and/not between and
#案例1:查询部门编号是30-90之间的部门编号、员工姓名
select first_name , department_id
from employees
where department_id BETWEEN 30 AND 90;

#案例2:查询年薪不是100000-200000之间的员工姓名、工资、年薪
-- IFNULL(expr1,expr2) expr1可能出现null值的表达式;expr2是若expr1是null值则显示的值
select first_name , commission_pct from employees;
select first_name , IFNULL(commission_pct,0) from employees;

select first_name , salary , salary*(1+IFNULL(commission_pct,0))*12 '年薪'
from employees
where salary*(1+IFNULL(commission_pct,0))*12 not between 100000 and 200000;

-- 六、查询是null字段  is null , 查询不是null字段  is not null
#案例1:查询没有奖金的员工信息
select * from employees where commission_pct is null;
#案例2:查询有奖金的员工信息
select * from employees where commission_pct is not null;

#数值 等于判断
select * from employees where salary = 9000.0;
#数值 不等于判断
select * from employees where salary <> 9000.0;
#数值 绝对等于判断
select * from employees where salary <=> 9000.0;

#ifnull(表达式1,表达式2)
/*
表达式1:可能为null的字段或表达式
表达式2:如果表达式1为null,则最终结果显示的值

功能:如果表达式1为null,则显示表达式2,否则显示表达式1
*/

#4.作业
-- 1. 查询工资大于 12000 的员工姓名和工资
select first_name , salary
from employees
where salary>12000;
-- 2. 查询员工号为 176 的员工的姓名和部门号和年薪
select employee_id , first_name , department_id , salary*12 '年薪'
from employees
where employee_id <=> 176;
-- 3. 选择工资不在 5000 到 12000 的员工的姓名和工资
select first_name , salary
from employees
where salary <5000 or salary >12000;
-- 4. 选择在 20 或 50 号部门工作的员工姓名和部门号
select first_name , department_id
from employees
where department_id in(20,50);

select first_name , department_id
from employees
where (department_id = 20) OR (department_id = 50);
-- 5. 选择公司中没有管理者的员工姓名及 job_id
select first_name , job_id
from employees
where manager_id is not null;
-- 6. 选择公司中有奖金的员工姓名,工资和奖金级别
select first_name , salary , commission_pct
from employees
where commission_pct is not null;
-- 7. 选择员工姓名的第三个字母是 a 的员工姓名
select first_name
from employees
where first_name like '__a%';
-- 8. 选择姓名中有字母 a 和 e 的员工姓名
select first_name
from employees
where first_name like '%a%' and first_name like '%e%';
-- 9. 显示出表 employees 表中 first_name 以 'e'结尾的员工信息
select *
from employees
where first_name like '%e';
-- 10. 显示出表 employees 部门编号在 80-100 之间 的姓名、职位
select first_name , job_id , department_id
from employees
where department_id Between 80 and 100;
-- 11. 显示出表 employees 的 manager_id 是 100,101,110 的员工姓名、职位
select first_name , job_id , manager_id
from employees
where manager_id IN (100,101,110);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值