基础语句-进阶2【条件查询】
-
条件查询
-
语法:
select 查询列表 from 表名 where 筛选条件; 执行顺序: 1. from 子句 2. where 子句 3. select 子句
-
特点
-
按关系表达式筛选
-
关系运算符: >,<, >=, <=, =, <>
-
# 1,查询部门编号不是100的员工信息 SELECT * FROM employees WHERE department_id <> 100; #2, 工资小于15000的姓名和工资 SELECT first_name,salary FROM employees WHERE salary < 15000;
-
-
按逻辑表达式筛选
-
与或非 and or not
-
#3, 查询部门编号不是50-100之间的员工姓名,部门编号,邮箱 SELECT last_name,department_id,email FROM employees WHERE NOT (department_id >= 50 AND department_id <= 100); #4,查询奖金率>0.03 或者 员工编号在60-110之间的员工信息 SELECT * FROM employees WHERE commission_pct > 0.03 OR (employee_id >=60 AND employee_id <=110);
-
-
模糊查询
-
like in between and is null
-
like
-
/* 功能:一般和通配符搭配使用,对字符型数据进行部分匹配查询 常见的通配符: _ 任意单个字符 % 任意多个字符 $ ESCAPE '$' 将$后面的字符转义 */ #5, 查询姓名中包含字符a的员工信息 SELECT * FROM employees WHERE last_name LIKE '%a%'; #6, 查询姓名中最后一个字符为e字符的员工信息 SELECT * FROM employees WHERE last_name LIKE '%e'; #7, 查询姓名中第三个字符为x字符的员工信息 SELECT * FROM employees WHERE last_name LIKE '__x%'; #5, 查询姓名中包含字符a的员工信息 SELECT * FROM employees WHERE last_name LIKE '%a%'; #6, 查询姓名中最后一个字符为e字符的员工信息 SELECT * FROM employees WHERE last_name LIKE '%e'; #7, 查询姓名中第三个字符为x字符的员工信息 SELECT * FROM employees WHERE last_name LIKE '__x%'; #8, 查询姓名中第二个字符为_字符的员工信息 【$作为转译字符的标准写法】 SELECT * FROM employees WHERE last_name LIKE '_$_%' ESCAPE '$';
-
-
in
-
/* 功能:查询某字段的值是否属于指定的列表之内 a in (常量1,常量2,常量3...) a not in (常量1,常量2,常量3...) */ #9, 查询部门编号是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; #10, 查询工种编号不是sh_clerk 或者it_prog的员工信息 SELECT * FROM employees WHERE job_id NOT IN ('SH_CLERK', 'IT_PROG');
-
-
between...and
-
/* 功能:判断某个字段的值是否介于。。。之间 */ #11, 查询部门编号是30-90之间的部门编号、员工姓名 SELECT department_id,last_name FROM employees WHERE department_id BETWEEN 30 AND 90; #12, 查询年薪不是10w~20w之间的员工姓名、工资、年薪 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;
-
-
is null
-
/* 功能:判断不为空 is 只能用来判断null = 只能用来判断普通的内容 <=> 安全等于,既能用来判断null 也能用来判断普通的内容 */ #13, 查询没有奖金的员工信息 SELECT * FROM employees WHERE commission_pct IS NULL; #14, 查询有奖金的员工信息 SELECT * FROM employees WHERE commission_pct IS NOT NULL;
-
-
-
-