# 进阶2:条件查询 /* 语法: select 查询列表 from 表名 where 筛选条件; 分类: 一、按照条件表达式筛选 条件运算符:> < = !=(等价于<>) >= <= 二、按照逻辑表达式筛选 逻辑运算符: 作用:用于连接条件表达式 && || ! and or not && 和 and:2个条件均为true,结果为true,反之为false || 和 or:只要一个为true,结果为true,反之为false ! 和 not:not true 为false 三、模糊查询 like between and in is null / is not null */ # 一、按照条件表达式筛选 # 案例1:查询员工工资>12000的员工信息 select * from employees where salary>12000; #案例2:查询部门编号不等于90号的员工名和部门编号 select first_name,last_name,department_id from employees where department_id != 90; #二、按照逻辑表达式筛选 # 案例1:查询工资在10000到20000之间的员工名、工资和奖金 select first_name,last_name,salary,commission_pct from employees where 10000<=salary and salary<=20000; #案例2:查询部门编号不是在90到110之间,或者工资高于15000的员工信息。 select * from employees where not (90<=department_id and manager_id<=110) or salary>15000; # 三、模糊查询 /* 1.like 和通配符搭配使用 通配符%:任意多个字符,包含0个字符 _: 任意单个字符 */ # 案例1:查询员工名中包含字符a的员工信息 select * from employees where last_name like "%a%"; #案例2:查询员工名中第三个字符为n,第五个字符为l的员工名和工资。 select first_name,last_name,salary from employees where last_name like "__n_l%"; #案例3:查询员工名中第二个字符为_的员工名(特殊字符需要转义) select last_name from employees where last_name like "_\_%"; #这里你也可以自定义转义 select last_name from employees where last_name like "_$_%" escape "$"; #2.between and /* (1) 使用between and 可以提高语句的简洁度 (2) 包含临界值 (3) 等价于<= and <= */ #案例1:查询员工编号在100到120之间的员工信息 select * from employees where employee_id between 100 and 120; select * from employees where 100<=employee_id and employee_id<=120; #3. in /* 含义:判断某字段的值是否属于in列表中的某一项 特点: (1)使用In提高语句简洁度 (2)in列表的值需要一致 (3)in等价于= */ # 案例:查询员工的工种编号是AD_PRES,AD_VP,IT_PROG的一个员工名和工种编号 select last_name,job_id from employees where job_id = 'AD_PRES' or job_id = 'AD_VP' or job_id = 'IT_PROG'; select last_name,job_id from employees where job_id in('AD_PRES','AD_VP','IT_PROG'); # 4.is null /* 判断空值 */ # 案例1:查询没有奖金的员工名和奖金率 (=无法判断null值) select last_name,commission_pct from employees where commission_pct is null; select last_name,commission_pct from employees where commission_pct is not null; #安全等于 <=> # 案例1:查询没有奖金的员工名和奖金率 (=无法判断null值) select last_name,commission_pct from employees where commission_pct <=> null;