进阶2:条件查询
语法
select 查询列表 from 表名 where 筛选条件
分类
一、按条件表达式筛选
条件运算符:> < = != >= <=
二、按逻辑表达式筛选
逻辑运算符
&& and :两个条件都为true,结果为true,反之为false
|| or:只要有一个条件为true,结果为true,反之为false
! not :如果连接的条件本身为false,结果为true,反之为false
三、模糊查询
like
特点:
①一般和通配符搭配使用
通配符:
%任意多个字符
between and
in
is null
*/
1、按条件表达式筛选
案例1:查询工资大于12000的员工信息
select * from employees where sqlary>12000
select * from employees where not(department_id>=90 and department_id<=110) or salary >150000
/*
2、模糊查询
like
①一般通配符搭配使用
%任意多个字符,包含0个
_
*/
select * from 表名 where last_name LIKE ‘%a%’;
#案例2:查询员工中第三个字符为e,第五个字符为a的员工名和工资
select last_name,salary from employees where last_name like ‘__e_a%’
#案例3:查询员工名中第二个字符为_的员工名
select last_name from employees where last_name like ‘__%’ escape ‘’;
3.between and
/*
①使用between and 可以提高语句的简洁度
②包含临界值
③两个临界值不要调换顺序
*/
select * from employees between 100 and 120;
4.in
/*
含义:判断某字段的值是否属于in列表中的某一项
特点:
①使用in比or更简洁
②in列表的值类型必须一致或者兼容
*/
#查询:查询员工的工种编号是 IT_PROG、AD_VP的一个员工名和工种编号
select last_name,job_id from employees where job_id in(‘IT_PROG’,‘AD_VP’)
5.is null
/*
=或<>不能用于判断null值
is null 或 is not null 可以判断null值
*/
#案例1:查询没有奖金的员工名和奖金率
select last_name,commission_pct from employees where commission_pct is null
安全等于<=>
select last_name,commission_pct from employees where commission_pct <=> null
/*
is null:仅仅可以判断null值,可读性高
<=>:既可以判断null值,又可以判断普通的数值,可读性较低
*/