09.DQL语言之条件查询

一、语法

select 查询列表
from 表名
where 筛选条件;

二、执行顺序

先执行from子句,再执行where子句,最后执行select子句。

三、筛选条件的分类

1、按条件表达式筛选
简单条件运算符:<,>,=,!=或者<>(建议使用),>= ,<=
复杂条件用算符:
like:一般搭配通配符使用,可以判断字符型或数值型
通配符:%代表任意多个字符,包含0个字符,_代表任意单个字符
between and
in和not in:判断某字段的值是否属于in列表中的某一项
is null和is not null:用于判断某个值是否等于null
2、按逻辑表达式筛选
逻辑运算符:&& || !推荐使用 and or not
&&或and:全true则true
||或or:有true则true
!或not:遇true则false,遇false则true
逻辑运算符的作用:用于连接条件表达式

四、案例

案例1:查询工资大于12000的员工信息

select * 
from employees 
where salary > 12000

案例2:查询部门编号不等于90号的员工名和部门编号

select last_name,department_id 
from employees 
where department_id <> 90;

案例3:查询工资在10000到20000之间的员工名、工资以及奖金率

select  last_name,salary,commission_pct 
from employees 
where salary>=10000 and salary<=20000;

案例4:查询部门编号不在90到110之间,或者工资高于15000的员工信息

select * 
from employees
where not (department_id>=90 and department_id <= 110 ) or salary >15000;

案例5:查询员工名中包含字符a的员工信息

select * 
from employees 
where last_name like '%a%';

案例6:查询员工名中第三个字符为e,第5个字符为a的员工名和工资

select last_name,salary 
from employees 
where last_name like '__e_a%';

案例7: 查询员工名中第二个字符为_的员工名

-- 考察通配符转义
select last_name
from employees
where last_name like '_\_%';
-----------------------------------------
推荐使用关键字:escape 来定义转义字符
select last_name
from employees
where last_name like '_$_%' escape '$';

案例8:查询员工编号在100到120之间的员工信息

select *
from employees
where employee_id between 100 and 120;
-- 等价于
select *
from employees
where employee_id>=100 and employee__id <=120;
-- 使用 between and 可以提高语句的简洁度
-- 包含临界值
-- 两个临界值的顺序不可调换

案例9:查询员工的工种编号是 IT_PROG或AD_VP或AD_PRES的员工名和工种编号

select last_name,job_id
from employees
where job_id in('IT_PROG','AD_VP','AD_PRES');
-- 使用in提高语句简洁度
-- in列表的值类型必须一致或兼容,所谓兼容,指的是可以相互转换,例如123和'123'是兼容的
-- in列表中的值不支持通配符

案例10:查询没有奖金的员工名和奖金率

 select last_name,commission_pct
 from employees
 where commission_pct is null;
 -- 判断某个值是否等于null,只能用is null,而不能用 = null
 -- =或者<>不能用于判断null值
 -- is null或is not null 可以判断null值。

五、安全等于 PK is null

is null : 仅仅可以判断null值,可读性高------推荐使用
<=>:既可以判断null值,又可以判断普通的数值,可读性低


案例1:查找员工奖金率为 0.25 的员工名以及奖金率

select last_name,commission_pct from employees 
where commission_pct <=> 0.25;

案例2:查询没有奖金的员工名和奖金率

select last_name,commission_pct
 from employees
 where commission_pct <=> null;

六、综合案例

查询工号为176的员工姓名,部门编号和年薪

select concat(first_name,last_name) 姓名,
       department_id 部门编号,
       salary*12*(1+ifnull(commission_pct,0)) 年薪
from employees
where employee_id = 176;

七、测试题

1、查询没有奖金,且工资小于18000的salary,last name

select last_name,salary
from employees
where commission_pct is null and salary < 18000;

2、查询employees表中,job.id不为IT或者工资为12000的员工信息

select *
from employees
where job_id not in('IT') or salary = 12000;

3、查看部门表departments的表结构

desc departments;

4、查询部门departments表中涉及到了哪些位置编号

select distinct location_id
from departments;

八、经典面试题

select * from employees;和 select * from employees where commission_pct like ‘%%’ and last_name like ‘%%’;


答:不一样,因为commission_pct中有null值,而null并不等于’’,而’%%‘最少零个字符的情况为’’

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值