条件查询(关系、逻辑表达式、模糊查询)
进阶2 条件查询
/*
语法:
select 查询列表
from 表名
where 筛选条件
执行顺序:
(1)from子句
(2)where子句
(3)select子句
select last_name,first_name from employees where salary>20000;
特点:
1、按关系表达式筛选
关系运算符: > < >= <= = <>(是不等于的意思)
补充:也可以使用!= ,但是不建议
2、按逻辑表达式筛选: and or not
补充:也可以使用 && || !
3、模糊查询
like
in
between and
is null
*/
#一、按关系表达式筛选
#案例一:查询部门编号不是100的员工信息
SELECT *
FROM `employees`
WHERE `department_id` <> 100;
#案例二:查询工资<15000的姓名、工资
SELECT `first_name`,`last_name`,`salary`
FROM `employees`
WHERE `salary`<15000;
#二、按逻辑表达式筛选
#案例一:查询部门编号不是50-100之间员工姓名、部门编号、邮箱
#方式一
SELECT `first_name`,`last_name`,`department_id`,`email`
FROM `employees`
WHERE `department_id` NOT BETWEEN 50 AND 100;
#方式二
SELECT `first_name`,`last_name`,`department_id`,`email`
FROM `employees`
WHERE NOT(`department_id`>=50 AND `department_id`<=100);
#方式三:
SELECT `first_name`,`last_name`,`department_id`,`email`
FROM `employees`
WHERE `department_id`<50 OR `department_id`>100;
#案例二:
#方式一:
SELECT *
FROM employees
WHERE commission_pct>0.03 OR (employee_id>=60 AND employee_id>=110);
#方式二:
SELECT *
FROM employees
WHERE commission_pct>0.03 OR (employee_id NOT BETWEEN 60 AND 110);
#三、模糊查询
#1、like
# like / not like
/*
功能:一般和通配符搭配使用,对字符型数据进行部分匹配查询
常见的通配符:
_ 任意单个字符
% 任意多个字符
*/
#实例1:查询姓名中包含字符a的员工信息
SELECT *
FROM `employees`
WHERE last_name LIKE '%a%';
#实例2:查询姓名中包含最后一个字符为e的员工信息
SELECT *
FROM `employees`
WHERE last_name LIKE '%e';
#实例3:查询姓名中包含第一个字符为e的员工信息
SELECT *
FROM `employees`
WHERE last_name LIKE 'e%';
#实例4:查询姓名中包含第三个字符为x的员工信息
SELECT *
FROM `employees`
WHERE last_name LIKE '__x%';
#实例5:查询姓名中包含第二个字符为_的员工信息
#方式一:(转义字符\,类似与Java中的转义字符)
SELECT *
FROM `employees`
WHERE last_name LIKE '_\_%';
#方式二:
SELECT *
FROM `employees`
WHERE last_name LIKE '_$_%' ESCAPE '$';
#方式二的另外一种写法,$符可以改用任意一个字符
SELECT *
FROM `employees`
WHERE last_name LIKE '_a_%' ESCAPE 'a';
#2、in
/*
功能:查询某字段的值是否属于指定的列表之内
a in(常量值1,常量值2,常量值3,....)
a not in(常量值1,常量值2,常量值3,....)
*/
#案例1:查询部门编号是30/50/90的员工明、部门编号
#方式一:
SELECT `last_name`,`department_id`
FROM `employees`
WHERE `department_id` IN (30,50,90);
#方式二:
SELECT `last_name`,`department_id`
FROM `employees`
WHERE `department_id`=30 OR `department_id`=50 OR `department_id`=90;
#案例二:查询工种编号不是SH_CLERK或IT_PROG的员工信息
SELECT *
FROM `employees`
WHERE `job_id` NOT IN('SH_CLERK','IT_PROG');
#3、between and
/*
功能:判断某个字段的值是否介于xx之间
between and /not between and
*/
#案例一:查询部门编号是30-90之间的部门编号、员工姓名
#方式一:
SELECT `department_id`,`last_name`
FROM `employees`
WHERE `department_id` BETWEEN 30 AND 90;
#方式二:
SELECT `department_id`,`last_name`
FROM `employees`
WHERE `department_id`>= 30 AND `department_id`<=90;
#案例2:查询年薪不是100000-200000之间的员工姓名、工资、年薪
#方式一
SELECT `last_name`,`salary`,`salary`*12*(1+IFNULL(`commission_pct`,0)) AS 年薪
FROM `employees`
WHERE `salary`*12*(1+IFNULL(`commission_pct`,0)) NOT BETWEEN 100000 AND 200000;
SELECT `last_name`,`salary`,`salary`*12*(1+IFNULL(`commission_pct`,0)) 年薪
FROM `employees`
WHERE `salary`*12*(1+IFNULL(`commission_pct`,0)) NOT BETWEEN 100000 AND 200000;
#方式二
SELECT `last_name`,`salary`,`salary`*12*(1+IFNULL(`commission_pct`,0)) AS 年薪
FROM `employees`
WHERE `salary`*12*(1+IFNULL(`commission_pct`,0))<100000 OR `salary`*12*(1+IFNULL(`commission_pct`,0))>200000;