一、基础查询
1、语法:select 查询列表 from 表名;
2、特点:① 通过select查询完的结果,是一个虚拟的表格,不是真实存在
② 查询列表可以是:字段、表达式、常量、函数等
③ select后面跟的查询列表,可以有多个部分组成,中间用逗号隔开
④ 使用前需打开指定的数据库,USE 表名;
执行顺序:① from子句 --> ② select子句
1 #一、查询常量 2 SELECT 100; 3 4 #二、查询表达式 5 SELECT 100%3; 6 7 #三、查询单个字段 8 SELECT `last_name` FROM `employees`; 9 10 #四、查询多个字段 11 SELECT `last_name`,`email`,`employee_id` FROM employees; 12 13 #五、查询所有字段 14 SELECT * FROM `employees`; 15 16 #F12:对齐格式 17 SELECT 18 `last_name`, 19 `first_name`, 20 `last_name`, 21 `commission_pct`, 22 `hiredate`, 23 `salary` 24 FROM 25 employees ; 26 27 #六、查询函数(调用函数,获取返回值) 28 SELECT DATABASE(); 29 SELECT VERSION(); 30 SELECT USER(); 31 32 #七、起别名 33 #方式一:使用as关键字 34 35 SELECT USER() AS 用户名; 36 SELECT USER() AS "用户名"; 37 SELECT USER() AS '用户名'; 38 39 SELECT last_name AS "姓 名" FROM employees; 40 41 #方式二:使用空格 42 43 SELECT USER() 用户名; 44 SELECT USER() "用户名"; 45 SELECT USER() '用户名'; 46 47 SELECT last_name "姓 名" FROM employees; 48 49 50 #八、+的作用 51 -- 需求:查询 first_name 和last_name 拼接成的全名,最终起别名为:姓 名 52 53 #方案1:使用+ pass× 54 SELECT first_name+last_name AS "姓 名" FROM employees; 55 56 #方案2:使用concat拼接函数 57 SELECT CONCAT(first_name,last_name) AS "姓 名" FROM employees; 58 59 /* 60 61 Java中+的作用: 62 1、加法运算 63 100+1.5 'a'+2 1.3+'2' 64 65 2、拼接符 66 至少有一个操作数为字符串 67 "hello"+'a' 68 69 70 mysql中+的作用: 71 1、加法运算 72 73 ①两个操作数都是数值型 74 100+1.5 75 76 ②其中一个操作数为字符型 77 将字符型数据强制转换成数值型,如果无法转换,则直接当做0处理 78 79 '张无忌'+100===>100 80 81 82 ③其中一个操作数为null 83 84 null+null====》null 85 86 null+100====》 null 87 88 */ 89 90 #九、distinct的使用 91 92 #需求:查询员工涉及到的部门编号有哪些 93 SELECT DISTINCT department_id FROM employees; 94 95 #十、查看表的结构 96 97 DESC employees; 98 SHOW COLUMNS FROM employees;
二、条件查询
1、语法:select 查询列表 from 表名 where 筛选条件;
2、执行顺序:①from子句 --> ②where子句 --> ③select子句
3、特点:① 按关系表达式筛选:关系运算符:> < >= <= = <>
② 按逻辑表达式筛选:逻辑运算符:and or not
③ 模糊查询:like、in、between and、is null
1 #一、按关系表达式筛选 2 #案例1:查询部门编号不是100的员工信息 3 SELECT * FROM employees WHERE department_id <> 100; 4 5 #案例2:查询工资<15000的姓名、工资 6 SELECT last_name,salary FROM employees WHERE salary<15000; 7 8 #二、按逻辑表达式筛选 9 10 #案例1:查询部门编号不是 50-100之间员工姓名、部门编号、邮箱 11 #方式1: 12 SELECT last_name,department_id,email FROM employees WHERE department_id <50 OR department_id>100; 13 14 #方式2: 15 SELECT last_name,department_id,email FROM employees WHERE NOT(department_id>=50 AND department_id<=100); 16 17 #案例2:查询奖金率>0.03 或者 员工编号在60-110之间的员工信息 18 SELECT * FROM employees WHERE commission_pct>0.03 OR (employee_id >=60 AND employee_id<=110); 19 20 #三、模糊查询 21 #1、like 22 /* 23 功能:一般和通配符搭配使用,对字符型数据进行部分匹配查询 24 常见的通配符: 25 _ 任意单个字符 26 % 任意多个字符,支持0-多个 27 like/not like 28 */ 29 #案例1:查询姓名中包含字符a的员工信息 30 SELECT * FROM employees WHERE last_name LIKE '%a%'; 31 32 #案例2:查询姓名中包含最后一个字符为e的员工信息 33 SELECT * FROM employees WHERE last_name LIKE '%e'; 34 35 #案例3:查询姓名中包含第一个字符为e的员工信息 36 SELECT * FROM employees WHERE last_name LIKE 'e%'; 37 38 #案例4:查询姓名中包含第三个字符为x的员工信息 39 SELECT * FROM employees WHERE last_name LIKE '__x%'; 40 41 #案例5:查询姓名中包含第二个字符为_的员工信息 42 SELECT * FROM employees WHERE last_name LIKE '_\_%'; 43 44 SELECT * FROM employees WHERE last_name LIKE '_$_%' ESCAPE '$'; 45 46 #2、in 47 /* 48 功能:查询某字段的值是否属于指定的列表之内 49 50 a in(常量值1,常量值2,常量值3,...) 51 a not in(常量值1,常量值2,常量值3,...) 52 53 in/not in 54 */ 55 56 #案例1:查询部门编号是30/50/90的员工名、部门编号 57 58 #方式1: 59 SELECT last_name,department_id FROM employees WHERE department_id IN(30,50,90); 60 61 #方式2: 62 SELECT last_name,department_id 63 FROM employees 64 WHERE department_id = 30 65 OR department_id = 50 66 OR department_id = 90; 67 68 #案例2:查询工种编号不是SH_CLERK或IT_PROG的员工信息 69 #方式1: 70 SELECT * FROM employees WHERE job_id NOT IN('SH_CLERK','IT_PROG'); 71 72 #方式2: 73 SELECT * FROM employees WHERE NOT(job_id ='SH_CLERK' OR job_id = 'IT_PROG'); 74 75 #3、between and 76 /* 77 功能:判断某个字段的值是否介于xx之间 78 79 between and/not between and 80 81 */ 82 #案例1:查询部门编号是30-90之间的部门编号、员工姓名 83 84 #方式1: 85 SELECT department_id,last_name FROM employees WHERE department_id BETWEEN 30 AND 90; 86 87 #方式2: 88 89 SELECT department_id,last_name FROM employees WHERE department_id>=30 AND department_id<=90; 90 91 #案例2:查询年薪不是100000-200000之间的员工姓名、工资、年薪 92 SELECT last_name,salary,salary*12*(1+IFNULL(commission_pct,0)) 年薪 93 FROM employees 94 WHERE salary*12*(1+IFNULL(commission_pct,0))<100000 OR salary*12*(1+IFNULL(commission_pct,0))>200000; 95 96 SELECT last_name,salary,salary*12*(1+IFNULL(commission_pct,0)) 年薪 97 FROM employees 98 WHERE salary*12*(1+IFNULL(commission_pct,0)) NOT BETWEEN 100000 AND 200000; 99 100 #4、is null/is not null 101 102 #案例1:查询没有奖金的员工信息 103 SELECT * FROM employees WHERE commission_pct IS NULL; 104 105 #案例2:查询有奖金的员工信息 106 SELECT * FROM employees WHERE commission_pct IS NOT NULL; 107 108 SELECT * FROM employees WHERE salary IS 10000; 109 110 #----------------对比------------------------------------ 111 112 = 只能判断普通的内容 113 114 IS 只能判断NULL值 115 116 <=> 安全等于,既能判断普通内容,又能判断NULL值 117 118 SELECT * FROM employees WHERE salary <=> 10000; 119 120 SELECT * FROM employees WHERE commission_pct <=> NULL;
三、排序查询
1、语法:select 查询列表 from 表名【where 筛选条件】 order by 排序列表
2、执行顺序:①from子句 ②where子句 ③select子句 ④order by 子句
3、特点:①、排序列表可以是单个字段、多个字段、表达式、函数、列数、以及以上的组合
②、升序,通过asc,默认行为降序,通过desc
1 #一、按单个字段排序 2 #案例1:将员工编号>120的员工信息进行工资的升序 3 SELECT * FROM employees ORDER BY salary ; 4 5 #案例1:将员工编号>120的员工信息进行工资的降序 6 SELECT * FROM employees WHERE employee_id>120 ORDER BY salary DESC; 7 8 #二、按表达式排序 9 #案例1:对有奖金的员工,按年薪降序 10 SELECT *,salary*12*(1+IFNULL(commission_pct,0)) 年薪 11 FROM employees 12 WHERE commission_pct IS NOT NULL 13 ORDER BY salary*12*(1+IFNULL(commission_pct,0)) DESC; 14 15 #三、按别名排序 16 #案例1:对有奖金的员工,按年薪降序 17 SELECT *,salary*12*(1+IFNULL(commission_pct,0)) 年薪 FROM employees ORDER BY 年薪 DESC; 18 19 #四、按函数的结果排序 20 21 #案例1:按姓名的字数长度进行升序 22 SELECT last_name FROM employees ORDER BY LENGTH(last_name); 23 24 #五、按多个字段排序 25 #案例1:查询员工的姓名、工资、部门编号,先按工资升序,再按部门编号降序 26 27 SELECT last_name,salary,department_id 28 FROM employees 29 ORDER BY salary ASC,department_id DESC; 30 31 32 #六、补充选学:按列数排序 33 SELECT * FROM employees ORDER BY 2 DESC; 34 35 SELECT * FROM employees ORDER BY first_name;
四、常见函数
1、字符函数
1 1、CONCAT 拼接字符 2 3 SELECT CONCAT('hello,',first_name,last_name) 备注 FROM employees; 4 5 2、LENGTH 获取字节长度 6 7 SELECT LENGTH('hello,郭襄'); 8 9 3、CHAR_LENGTH 获取字符个数 10 SELECT CHAR_LENGTH('hello,郭襄'); 11 12 4、SUBSTRING 截取子串 13 /* 14 注意:起始索引从1开始!!! 15 substr(str,起始索引,截取的字符长度) 16 substr(str,起始索引) 17 */ 18 SELECT SUBSTR('张三丰爱上了郭襄',1,3); 19 SELECT SUBSTR('张三丰爱上了郭襄',7); 20 21 5、INSTR获取字符第一次出现的索引 22 23 SELECT INSTR('三打白骨精aaa白骨精bb白骨精','白骨精'); 24 25 6、TRIM去前后指定的字符,默认是去空格 26 27 28 SELECT TRIM(' 虚 竹 ') AS a; 29 SELECT TRIM('x' FROM 'xxxxxx虚xxx竹xxxxxxxxxxxxxxxxxx') AS a; 30 31 7、LPAD/RPAD 左填充/右填充 32 SELECT LPAD('木婉清',10,'a'); 33 SELECT RPAD('木婉清',10,'a'); 34 35 8、UPPER/LOWER 变大写/变小写 36 37 #案例:查询员工表的姓名,要求格式:姓首字符大写,其他字符小写,名所有字符大写,且姓和名之间用_分割,最后起别名“OUTPUT” 38 39 40 SELECT UPPER(SUBSTR(first_name,1,1)),first_name FROM employees; 41 SELECT LOWER(SUBSTR(first_name,2)),first_name FROM employees; 42 SELECT UPPER(last_name) FROM employees; 43 44 SELECT CONCAT(UPPER(SUBSTR(first_name,1,1)),LOWER(SUBSTR(first_name,2)),'_',UPPER(last_name)) "OUTPUT" 45 FROM employees; 46 47 9、STRCMP 比较两个字符大小 48 49 SELECT STRCMP('aec','aec'); 50 51 10、LEFT/RIGHT 截取子串 52 SELECT LEFT('鸠摩智',1); 53 SELECT RIGHT('鸠摩智',1);
2、数学函数
1 1、ABS 绝对值 2 SELECT ABS(-2.4); 3 2、CEIL 向上取整 返回>=该参数的最小整数 4 SELECT CEIL(-1.09); 5 SELECT CEIL(0.09); 6 SELECT CEIL(1.00); 7 8 3、FLOOR 向下取整,返回<=该参数的最大整数 9 SELECT FLOOR(-1.09); 10 SELECT FLOOR(0.09); 11 SELECT FLOOR(1.00); 12 13 4、ROUND 四舍五入 14 SELECT ROUND(1.8712345); 15 SELECT ROUND(1.8712345,2); 16 17 5、TRUNCATE 截断 18 19 SELECT TRUNCATE(1.8712345,1); 20 21 6、MOD 取余 22 23 SELECT MOD(-10,3); 24 a%b = a-(INT)a/b*b 25 -10%3 = -10 - (-10)/3*3 = -1 26 27 SELECT -10%3; 28 SELECT 10%3; 29 SELECT -10%-3; 30 SELECT 10%-3;
3、日期函数
1 1、NOW 2 SELECT NOW(); 3 4 2、CURDATE 5 6 SELECT CURDATE(); 7 8 3、CURTIME 9 SELECT CURTIME(); 10 11 4、DATEDIFF 12 SELECT DATEDIFF('1998-7-16','2019-7-13'); 13 14 5、DATE_FORMAT 15 16 SELECT DATE_FORMAT('1998-7-16','%Y年%M月%d日 %H小时%i分钟%s秒') 出生日期; 17 18 SELECT DATE_FORMAT(hiredate,'%Y年%M月%d日 %H小时%i分钟%s秒')入职日期 19 FROM employees; 20 21 6、STR_TO_DATE 按指定格式解析字符串为日期类型 22 SELECT * FROM employees 23 WHERE hiredate<STR_TO_DATE('3/15 1998','%m/%d %Y');
4、流程控制函数
1 1、IF函数 2 SELECT IF(100>9,'好','坏'); 3 4 #需求:如果有奖金,则显示最终奖金,如果没有,则显示0 5 SELECT IF(commission_pct IS NULL,0,salary*12*commission_pct) 奖金,commission_pct 6 FROM employees; 7 8 2、CASE函数 9 10 ①情况1 :类似于switch语句,可以实现等值判断 11 CASE 表达式 12 WHEN 值1 THEN 结果1 13 WHEN 值2 THEN 结果2 14 ... 15 ELSE 结果n 16 END 17 18 案例: 19 部门编号是30,工资显示为2倍 20 部门编号是50,工资显示为3倍 21 部门编号是60,工资显示为4倍 22 否则不变 23 24 显示 部门编号,新工资,旧工资 25 26 SELECT department_id,salary, 27 CASE department_id 28 WHEN 30 THEN salary*2 29 WHEN 50 THEN salary*3 30 WHEN 60 THEN salary*4 31 ELSE salary 32 END newSalary 33 FROM employees; 34 35 ②情况2:类似于多重IF语句,实现区间判断 36 CASE 37 WHEN 条件1 THEN 结果1 38 WHEN 条件2 THEN 结果2 39 ... 40 ELSE 结果n 41 END 42 43 案例:如果工资>20000,显示级别A 44 工资>15000,显示级别B 45 工资>10000,显示级别C 46 否则,显示D 47 48 SELECT salary, 49 CASE 50 WHEN salary>20000 THEN 'A' 51 WHEN salary>15000 THEN 'B' 52 WHEN salary>10000 THEN 'C' 53 ELSE 'D' 54 END 55 AS a 56 FROM employees;
五、分组函数
1、说明:分组函数往往用于实现将一组数据进行统计计算,最终得到一个值,又称为聚合函数或统计函数
2、分组函数清单:
sum(字段名):求和
avg(字段名):求平均数
max(字段名):求最大值
min(字段名):求最小值
count(字段名):计算非空字段值的个数
1 #案例1 :查询员工信息表中,所有员工的工资和、工资平均值、最低工资、最高工资、有工资的个数 2 3 SELECT SUM(salary),AVG(salary),MIN(salary),MAX(salary),COUNT(salary) FROM employees; 4 5 #案例2:添加筛选条件 6 #①查询emp表中记录数: 7 SELECT COUNT(employee_id) FROM employees; 8 9 #②查询emp表中有佣金的人数: 10 11 SELECT COUNT(salary) FROM employees; 12 13 #③查询emp表中月薪大于2500的人数: 14 SELECT COUNT(salary) FROM employees WHERE salary>2500; 15 16 17 #④查询有领导的人数: 18 SELECT COUNT(manager_id) FROM employees; 19 20 #count的补充介绍★ 21 22 #1、统计结果集的行数,推荐使用count(*) 23 24 SELECT COUNT(*) FROM employees; 25 SELECT COUNT(*) FROM employees WHERE department_id = 30; 26 27 SELECT COUNT(1) FROM employees; 28 SELECT COUNT(1) FROM employees WHERE department_id = 30; 29 30 #2、搭配distinct实现去重的统计 31 32 #需求:查询有员工的部门个数 33 34 SELECT COUNT(DISTINCT department_id) FROM employees; 35 36 #思考:每个部门的总工资、平均工资? 37 38 SELECT SUM(salary) FROM employees WHERE department_id = 30; 39 SELECT SUM(salary) FROM employees WHERE department_id = 50; 40 41 SELECT SUM(salary) ,department_id 42 FROM employees 43 GROUP BY department_id;
六、分组查询
1、语法:select 查询列表 from 表名 where 筛选条件 group by 分组列表 having 分组后筛选 order by 排序列表;
2、执行顺序:①from子句 ②where子句 ③group by 子句 ④having子句 ⑤select子句 ⑥order by子句
3、特点:① 查询列表往往是 分组函数和被分组的字段 ★
② 分组函数做条件只可能放在having后面!!!
③ 分组查询中的筛选分为两类
筛选的基表 | 使用的关键词 | 位置 | |
分组前筛选 | 原始表 | where | group by 的前面 |
分组后筛选 | 分组后的结果集 | having | group by的后面 |
1 #1)简单的分组 2 #案例1:查询每个工种的员工平均工资 3 4 SELECT AVG(salary),job_id FROM employees GROUP BY job_id; 5 6 #案例2:查询每个领导的手下人数 7 8 SELECT COUNT(*),manager_id 9 FROM employees 10 WHERE manager_id IS NOT NULL 11 GROUP BY manager_id; 12 13 #2)可以实现分组前的筛选 14 #案例1:查询邮箱中包含a字符的 每个部门的最高工资 15 SELECT MAX(salary) 最高工资,department_id 16 FROM employees 17 WHERE email LIKE '%a%' 18 GROUP BY department_id; 19 20 #案例2:查询每个领导手下有奖金的员工的平均工资 21 SELECT AVG(salary) 平均工资,manager_id 22 FROM employees 23 WHERE commission_pct IS NOT NULL 24 GROUP BY manager_id; 25 26 #3)可以实现分组后的筛选 27 #案例1:查询哪个部门的员工个数>5 28 #分析1:查询每个部门的员工个数 29 SELECT COUNT(*) 员工个数,department_id 30 FROM employees 31 GROUP BY department_id 32 33 #分析2:在刚才的结果基础上,筛选哪个部门的员工个数>5 34 35 SELECT COUNT(*) 员工个数,department_id 36 FROM employees 37 38 GROUP BY department_id 39 HAVING COUNT(*)>5; 40 41 #案例2:每个工种有奖金的员工的最高工资>12000的工种编号和最高工资 42 43 SELECT job_id,MAX(salary) 44 FROM employees 45 WHERE commission_pct IS NOT NULL 46 GROUP BY job_id 47 HAVING MAX(salary)>12000; 48 49 #案例3:领导编号>102的 每个领导手下的最低工资大于5000的最低工资 50 #分析1:查询每个领导手下员工的最低工资 51 SELECT MIN(salary) 最低工资,manager_id 52 FROM employees 53 GROUP BY manager_id; 54 55 #分析2:筛选刚才1的结果 56 SELECT MIN(salary) 最低工资,manager_id 57 FROM employees 58 WHERE manager_id>102 59 GROUP BY manager_id 60 HAVING MIN(salary)>5000 ; 61 62 #4)可以实现排序 63 #案例:查询没有奖金的员工的最高工资>6000的工种编号和最高工资,按最高工资升序 64 #分析1:按工种分组,查询每个工种有奖金的员工的最高工资 65 SELECT MAX(salary) 最高工资,job_id 66 FROM employees 67 WHERE commission_pct IS NULL 68 GROUP BY job_id 69 70 #分析2:筛选刚才的结果,看哪个最高工资>6000 71 SELECT MAX(salary) 最高工资,job_id 72 FROM employees 73 WHERE commission_pct IS NULL 74 GROUP BY job_id 75 HAVING MAX(salary)>6000 76 77 #分析3:按最高工资升序 78 SELECT MAX(salary) 最高工资,job_id 79 FROM employees 80 WHERE commission_pct IS NULL 81 GROUP BY job_id 82 HAVING MAX(salary)>6000 83 ORDER BY MAX(salary) ASC; 84 85 #5)按多个字段分组 86 #案例:查询每个工种每个部门的最低工资,并按最低工资降序 87 #提示:工种和部门都一样,才是一组 88 89 工种 部门 工资 90 1 10 10000 91 1 20 2000 92 2 20 93 3 20 94 1 10 95 2 30 96 2 20 97 98 SELECT MIN(salary) 最低工资,job_id,department_id 99 FROM employees 100 GROUP BY job_id,department_id;
七、连接查询
1、说明:又称多表查询,当查询语句涉及到的字段来自于多个表时,就会用到连接查询
2、笛卡尔乘积现象:表1 有m行,表2有n行,结果=m*n行
发生原因:没有有效的连接条件
如何避免:添加有效的连接条件
3、按功能分类(SQL99语法):
3.1 sql92标准:仅仅支持内连接:等值连接、非等值连接、自连接
1 #一、内连接 2 /* 3 语法: 4 select 查询列表 5 from 表1 别名,表2 别名 6 where 连接条件 7 and 筛选条件 8 group by 分组列表 9 having 分组后筛选 10 order by 排序列表 11 12 执行顺序: 13 1、from子句 14 2、where子句 15 3、and子句 16 4、group by子句 17 5、having子句 18 6、select子句 19 7、order by子句 20 */ 21 22 #一)等值连接 23 /* 24 ① 多表等值连接的结果为多表的交集部分 25 ②n表连接,至少需要n-1个连接条件 26 ③ 多表的顺序没有要求 27 ④一般需要为表起别名 28 ⑤可以搭配前面介绍的所有子句使用,比如排序、分组、筛选 29 */ 30 #案例1:查询女神名和对应的男神名 31 SELECT NAME,boyName 32 FROM boys,beauty 33 WHERE beauty.boyfriend_id= boys.id; 34 35 #案例2:查询员工名和对应的部门名 36 37 SELECT last_name,department_name 38 FROM employees,departments 39 WHERE employees.`department_id`=departments.`department_id`; 40 41 #2、为表起别名 42 /* 43 ①提高语句的简洁度 44 ②区分多个重名的字段 45 46 注意:如果为表起了别名,则查询的字段就不能使用原来的表名去限定 47 48 */ 49 #查询员工名、工种号、工种名 50 51 SELECT e.last_name,e.job_id,j.job_title 52 FROM employees e,jobs j 53 WHERE e.`job_id`=j.`job_id`; 54 55 #3、两个表的顺序是否可以调换 56 57 #查询员工名、工种号、工种名 58 59 SELECT e.last_name,e.job_id,j.job_title 60 FROM jobs j,employees e 61 WHERE e.`job_id`=j.`job_id`; 62 63 #4、可以加筛选 64 65 #案例:查询有奖金的员工名、部门名 66 67 SELECT last_name,department_name,commission_pct 68 69 FROM employees e,departments d 70 WHERE e.`department_id`=d.`department_id` 71 AND e.`commission_pct` IS NOT NULL; 72 73 #案例2:查询城市名中第二个字符为o的部门名和城市名 74 75 SELECT department_name,city 76 FROM departments d,locations l 77 WHERE d.`location_id` = l.`location_id` 78 AND city LIKE '_o%'; 79 80 #5、可以加分组 81 82 #案例1:查询每个城市的部门个数 83 84 SELECT COUNT(*) 个数,city 85 FROM departments d,locations l 86 WHERE d.`location_id`=l.`location_id` 87 GROUP BY city; 88 89 #案例2:查询有奖金的每个部门的部门名和部门的领导编号和该部门的最低工资 90 SELECT department_name,d.`manager_id`,MIN(salary) 91 FROM departments d,employees e 92 WHERE d.`department_id`=e.`department_id` 93 AND commission_pct IS NOT NULL 94 GROUP BY department_name,d.`manager_id`; 95 #6、可以加排序 96 97 #案例:查询每个工种的工种名和员工的个数,并且按员工个数降序 98 99 SELECT job_title,COUNT(*) 100 FROM employees e,jobs j 101 WHERE e.`job_id`=j.`job_id` 102 GROUP BY job_title 103 ORDER BY COUNT(*) DESC; 104 105 #7、可以实现三表连接? 106 107 #案例:查询员工名、部门名和所在的城市 108 109 SELECT last_name,department_name,city 110 FROM employees e,departments d,locations l 111 WHERE e.`department_id`=d.`department_id` 112 AND d.`location_id`=l.`location_id` 113 AND city LIKE 's%' 114 115 ORDER BY department_name DESC; 116 117 #二)非等值连接 118 119 120 #案例1:查询员工的工资和工资级别 121 122 SELECT salary,grade_level 123 FROM employees e,job_grades g 124 WHERE salary BETWEEN g.`lowest_sal` AND g.`highest_sal` 125 AND g.`grade_level`='A'; 126 127 #三)自连接 128 129 #案例:查询 员工名和上级的名称 130 131 SELECT e.employee_id,e.last_name,m.employee_id,m.last_name 132 FROM employees e,employees m 133 WHERE e.`manager_id`=m.`employee_id`;
3.2 sql99标准【推荐】:支持内连接+外连接(左外和右外)+交叉连接
① 内连接:等值连接、非等值连接、自连接
② 外连接:左外连接、右外连接、全外连接
③ 交叉连接
1 #一、内连接 2 语法: 3 SELECT 查询列表 4 FROM 表名1 别名 5 【INNER】 JOIN 表名2 别名 6 ON 连接条件 7 WHERE 筛选条件 8 GROUP BY 分组列表 9 HAVING 分组后筛选 10 ORDER BY 排序列表; 11 12 SQL92和SQL99的区别:SQL99使用JOIN关键字代替了之前的逗号,并且将连接条件和筛选条件进行了分离,提高阅读性!!! 13 14 #一)等值连接 15 #①简单连接 16 #案例:查询员工名和部门名 17 18 SELECT last_name,department_name 19 FROM departments d 20 JOIN employees e 21 ON e.department_id =d.department_id; 22 23 #②添加筛选条件 24 #案例1:查询部门编号>100的部门名和所在的城市名 25 SELECT department_name,city 26 FROM departments d 27 JOIN locations l 28 ON d.`location_id` = l.`location_id` 29 WHERE d.`department_id`>100; 30 31 #③添加分组+筛选 32 #案例1:查询每个城市的部门个数 33 34 SELECT COUNT(*) 部门个数,l.`city` 35 FROM departments d 36 JOIN locations l 37 ON d.`location_id`=l.`location_id` 38 GROUP BY l.`city`; 39 40 #④添加分组+筛选+排序 41 #案例1:查询部门中员工个数>10的部门名,并按员工个数降序 42 43 SELECT COUNT(*) 员工个数,d.department_name 44 FROM employees e 45 JOIN departments d 46 ON e.`department_id`=d.`department_id` 47 GROUP BY d.`department_id` 48 HAVING 员工个数>10 49 ORDER BY 员工个数 DESC; 50 51 52 #二)非等值连接 53 #案例:查询部门编号在10-90之间的员工的工资级别,并按级别进行分组 54 SELECT * FROM sal_grade; 55 56 SELECT COUNT(*) 个数,grade 57 FROM employees e 58 JOIN sal_grade g 59 ON e.`salary` BETWEEN g.`min_salary` AND g.`max_salary` 60 WHERE e.`department_id` BETWEEN 10 AND 90 61 GROUP BY g.grade; 62 63 64 #三)自连接 65 #案例:查询员工名和对应的领导名 66 67 SELECT e.`last_name`,m.`last_name` 68 FROM employees e 69 JOIN employees m 70 ON e.`manager_id`=m.`employee_id`; 71 72 #二、外连接 73 74 /* 75 76 说明:查询结果为主表中所有的记录,如果从表有匹配项,则显示匹配项;如果从表没有匹配项,则显示null 77 78 应用场景:一般用于查询主表中有但从表没有的记录 79 80 特点: 81 82 1、外连接分主从表,两表的顺序不能任意调换 83 2、左连接的话,left join左边为主表 84 右连接的话,right join右边为主表 85 86 语法: 87 88 select 查询列表 89 from 表1 别名 90 left|right|full 【outer】 join 表2 别名 91 on 连接条件 92 where 筛选条件; 93 94 */ 95 USE girls; 96 #案例1:查询所有女神记录,以及对应的男神名,如果没有对应的男神,则显示为null 97 98 #左连接 99 SELECT b.*,bo.* 100 FROM beauty b 101 LEFT JOIN boys bo ON b.`boyfriend_id` = bo.`id`; 102 103 #右连接 104 SELECT b.*,bo.* 105 FROM boys bo 106 RIGHT JOIN beauty b ON b.`boyfriend_id` = bo.`id`; 107 108 #案例2:查哪个女神没有男朋友 109 110 #左连接 111 SELECT b.`name` 112 FROM beauty b 113 LEFT JOIN boys bo ON b.`boyfriend_id` = bo.`id` 114 WHERE bo.`id` IS NULL; 115 116 #右连接 117 SELECT b.*,bo.* 118 FROM boys bo 119 RIGHT JOIN beauty b ON b.`boyfriend_id` = bo.`id` 120 WHERE bo.`id` IS NULL; 121 122 #案例3:查询哪个部门没有员工,并显示其部门编号和部门名 123 124 SELECT COUNT(*) 部门个数 125 FROM departments d 126 LEFT JOIN employees e ON d.`department_id` = e.`department_id` 127 WHERE e.`employee_id` IS NULL;
八、子查询
1、说明:当一个查询语句中又嵌套了另一个完整的select语句,则被嵌套的select语句称为子查询或内查询
外面的select语句称为主查询或外查询
2、按子查询出现的位置进行分类:
2.1 select后面
要求:子查询的结果为单行单列(标量子查询)
2.2 from后面
要求:子查询的结果可以为多行多列
2.3 where或having后面 ★
要求:子查询的结果必须为单列:单行子查询,多行子查询
2.4 exists后面
要求:子查询结果必须为单列(相关子查询)
1 #一、放在where或having后面 2 #一)单行子查询 3 4 #案例1:谁的工资比 Abel 高? 5 6 #①查询Abel的工资 7 SELECT salary 8 FROM employees 9 WHERE last_name = 'Abel' 10 #②查询salary>①的员工信息 11 SELECT last_name,salary 12 FROM employees 13 WHERE salary>( 14 SELECT salary 15 FROM employees 16 WHERE last_name <> 'Abel' 17 ); 18 19 #案例2:返回job_id与141号员工相同,salary比143号员工多的员工姓名,job_id 和工资 20 #①查询141号员工的job_id 21 SELECT job_id 22 FROM employees 23 WHERE employee_id = 141 24 25 #②查询143号员工的salary 26 27 SELECT salary 28 FROM employees 29 WHERE employee_id = 143 30 31 #③查询job_id=① and salary>②的信息 32 SELECT last_name,job_id,salary 33 FROM employees 34 WHERE job_id = ( 35 SELECT job_id 36 FROM employees 37 WHERE employee_id = 141 38 ) AND salary>( 39 40 SELECT salary 41 FROM employees 42 WHERE employee_id = 143 43 ); 44 45 #案例3:返回公司工资最少的员工的last_name,job_id和salary 46 47 #①查询最低工资 48 SELECT MIN(salary) 49 FROM employees 50 51 #②查询salary=①的员工的last_name,job_id和salary 52 SELECT last_name,job_id,salary 53 FROM employees 54 WHERE salary=( 55 SELECT MIN(salary) 56 FROM employees 57 ); 58 59 #案例4:查询最低工资大于50号部门最低工资的部门id和其最低工资 60 61 #①查询50号部门的最低工资 62 SELECT MIN(salary) 63 FROM employees 64 WHERE department_id = 50 65 66 67 #②查询各部门的最低工资,筛选看哪个部门的最低工资>① 68 69 SELECT MIN(salary),department_id 70 FROM employees 71 GROUP BY department_id 72 HAVING MIN(salary)>( 73 74 SELECT MIN(salary) 75 FROM employees 76 WHERE department_id = 50 77 ); 78 79 #二)多行子查询 80 /* 81 in:判断某字段是否在指定列表内 82 x in(10,30,50) 83 84 any/some:判断某字段的值是否满足其中任意一个 85 86 x>any(10,30,50) 87 x>min() 88 89 x=any(10,30,50) 90 x in(10,30,50) 91 92 93 all:判断某字段的值是否满足里面所有的 94 95 x >all(10,30,50) 96 x >max() 97 */ 98 99 #案例1:返回location_id是1400或1700的部门中的所有员工姓名 100 101 #①查询location_id是1400或1700的部门 102 SELECT department_id 103 FROM departments 104 WHERE location_id IN(1400,1700) 105 106 #②查询department_id = ①的姓名 107 SELECT last_name 108 FROM employees 109 WHERE department_id IN( 110 SELECT DISTINCT department_id 111 FROM departments 112 WHERE location_id IN(1400,1700) 113 ); 114 115 #题目:返回其它部门中比job_id为‘IT_PROG’部门任一工资低的员工的员工号、姓名、job_id 以及salary 116 117 #①查询job_id为‘IT_PROG’部门的工资 118 SELECT DISTINCT salary FROM employees WHERE job_id = 'IT_PROG' 119 120 #②查询其他部门的工资<任意一个①的结果 121 122 SELECT employee_id,last_name,job_id,salary 123 FROM employees 124 WHERE salary<ANY( 125 126 SELECT DISTINCT salary 127 FROM employees 128 WHERE job_id = 'IT_PROG' 129 ); 130 131 等价于 132 133 SELECT employee_id,last_name,job_id,salary 134 FROM employees 135 WHERE salary<( 136 137 SELECT MAX(salary) 138 FROM employees 139 WHERE job_id = 'IT_PROG' 140 ); 141 142 #案例3:返回其它部门中比job_id为‘IT_PROG’部门所有工资都低的员工 的员工号、姓名、job_id 以及salary 143 144 #①查询job_id为‘IT_PROG’部门的工资 145 SELECT DISTINCT salary 146 FROM employees 147 WHERE job_id = 'IT_PROG' 148 149 #②查询其他部门的工资<所有①的结果 150 151 SELECT employee_id,last_name,job_id,salary 152 FROM employees 153 WHERE salary<ALL( 154 155 SELECT DISTINCT salary 156 FROM employees 157 WHERE job_id = 'IT_PROG' 158 ); 159 160 等价于 161 162 SELECT employee_id,last_name,job_id,salary 163 FROM employees 164 WHERE salary<( 165 166 SELECT MIN(salary) 167 FROM employees 168 WHERE job_id = 'IT_PROG' 169 ); 170 171 #二、放在select后面 172 173 #案例;查询部门编号是50的员工个数 174 175 SELECT 176 ( 177 SELECT COUNT(*) 178 FROM employees 179 WHERE department_id = 50 180 ) 个数; 181 182 #三、放在from后面 183 184 #案例:查询每个部门的平均工资的工资级别 185 #①查询每个部门的平均工资 186 187 SELECT AVG(salary),department_id FROM employees GROUP BY department_id 188 189 #②将①和sal_grade两表连接查询 190 191 SELECT dep_ag.department_id,dep_ag.ag,g.grade 192 FROM sal_grade g 193 JOIN ( 194 195 SELECT AVG(salary) ag,department_id 196 FROM employees 197 GROUP BY department_id 198 199 ) dep_ag ON dep_ag.ag BETWEEN g.min_salary AND g.max_salary; 200 201 #四、放在exists后面 202 203 #案例1 :查询有无名字叫“张三丰”的员工信息 204 SELECT EXISTS( 205 SELECT * 206 FROM employees 207 WHERE last_name = 'Abel' 208 209 ) 有无Abel; 210 211 #案例2:查询没有女朋友的男神信息 212 213 USE girls; 214 215 SELECT bo.* 216 FROM boys bo 217 WHERE bo.`id` NOT IN( 218 SELECT boyfriend_id 219 FROM beauty b 220 ) 221 222 SELECT bo.* 223 FROM boys bo 224 WHERE NOT EXISTS( 225 SELECT boyfriend_id 226 FROM beauty b 227 WHERE bo.id = b.boyfriend_id 228 );
3、主查询与子查询是否相关进行分类:
3.1 非相关子查询:不依靠主查询,能够独立运行的子查询称为“非相关子查询”。如果子查询中仅仅使用了自己定义的数据源, 这种查询是非相关子查询。 非相关子查询是独立于外部查询的子查询, 子查询总共执行一次, 执行完
毕后将值传递给主查询。
执行过程:① 执行子查询,其结果不被显示,而是传递给外部查询,作为外部查询的条件使用。② 执行外部查询,并显示整个结果。
1 select * 2 from employees e 3 where department_id in ( 4 SELECT department_id 5 FROM departments d 6 WHERE d.department_id < 200 7 )
3.2 相关子查询:如果子查询中使用了主查询的数据源, 这种查询是相关子查询, 此时主查询的执行与相关子查询的执行相互依赖。
执行过程:① 从外层查询中取出一个元组,将元组相关列的值传递给内层查询
② 执行内层查询,得到子查询操作的值
③ 外查询根据子查询返回的结果或结果集得到满足条件的行
④ 然后外层查询取出下一个元组重复做步骤1-3,直到外层的元组全部处理完毕。
1 select * 2 from employees e 3 where department_id IN ( 4 SELECT department_id 5 FROM departments d 6 WHERE e.department_id = d.department_id 7 )
4、特点:
① 子查询放在条件中,要求必须放在条件的右侧
② 子查询一般放在小括号中
③ 子查询的执行优先于主查询
④ 单行子查询对应了 单行操作符:> < >= <= = <>
⑤ 多行子查询对应了 多行操作符:any/some all in
九、分页查询
1、语法:
1 select 查询列表 2 from 表1 别名 3 join 表2 别名 4 on 连接条件 5 where 筛选条件 6 group by 分组 7 having 分组后筛选 8 order by 排序列表 9 limit 起始条目索引,显示的条目数
2、执行顺序:
1》from子句 2》join子句 3》on子句 4》where子句 5》group by子句 6》having子句 7》select子句 8》order by子句 9》limit子句
3、特点:
①起始条目索引如果不写,默认是0
②limit后面支持两个参数
参数1:显示的起始条目索引
参数2:条目数
4、公式:假如要显示的页数是page,每页显示的条目数为size
select * from employees limit (page-1)*size,size;
1 #案例1:查询员工信息表的前5条 2 SELECT * FROM employees LIMIT 0,5; 3 #完全等价于 4 SELECT * FROM employees LIMIT 5; 5 6 #案例2:查询有奖金的,且工资较高的第11名到第20名 7 SELECT 8 * 9 FROM 10 employees 11 WHERE commission_pct IS NOT NULL 12 ORDER BY salary DESC 13 LIMIT 10,10 14 15 #练习:查询年薪最高的前10名 16 17 SELECT last_name,salary,salary*12*(1+IFNULL(commission_pct,0)) 年薪 18 FROM employees 19 ORDER BY 年薪 DESC 20 LIMIT 0,10;
十、联合查询
1、当查询结果来自于多张表,但多张表之间没有关联,这个时候往往使用联合查询,也称为union查询
2、语法:
select 查询列表 from 表1 where 筛选条件
union
select 查询列表 from 表2 where 筛选条件
3、特点:
3.1 多条待联合的查询语句的查询列数必须一致,查询类型、字段意义最好一致
3.2 union实现去重查询
union all 实现全部查询,包含重复项
1 #案例:查询所有国家的年龄>20岁的用户信息 2 3 SELECT * FROM usa WHERE uage >20 UNION 4 SELECT * FROM chinese WHERE age >20 ; 5 6 #案例2:查询所有国家的用户姓名和年龄 7 8 SELECT uname,uage FROM usa 9 UNION 10 SELECT age,`name` FROM chinese; 11 12 #案例3:union自动去重/union all 可以支持重复项 13 14 SELECT 1,'范冰冰' 15 UNION ALL 16 SELECT 1,'范冰冰' 17 UNION ALL 18 SELECT 1,'范冰冰' 19 UNION ALL 20 SELECT 1,'范冰冰';
数据结构:
/* SQLyog Ultimate v10.00 Beta1 MySQL - 5.5.15 : Database - myemployees ********************************************************************* */ /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; CREATE DATABASE /*!32312 IF NOT EXISTS*/`myemployees` /*!40100 DEFAULT CHARACTER SET gb2312 */; USE `myemployees`; /*Table structure for table `departments` */ DROP TABLE IF EXISTS `departments`; CREATE TABLE `departments` ( `department_id` int(4) NOT NULL AUTO_INCREMENT, `department_name` varchar(3) DEFAULT NULL, `manager_id` int(6) DEFAULT NULL, `location_id` int(4) DEFAULT NULL, PRIMARY KEY (`department_id`), KEY `loc_id_fk` (`location_id`), CONSTRAINT `loc_id_fk` FOREIGN KEY (`location_id`) REFERENCES `locations` (`location_id`) ) ENGINE=InnoDB AUTO_INCREMENT=271 DEFAULT CHARSET=gb2312; /*Data for the table `departments` */ insert into `departments`(`department_id`,`department_name`,`manager_id`,`location_id`) values (10,'Adm',200,1700),(20,'Mar',201,1800),(30,'Pur',114,1700),(40,'Hum',203,2400),(50,'Shi',121,1500),(60,'IT',103,1400),(70,'Pub',204,2700),(80,'Sal',145,2500),(90,'Exe',100,1700),(100,'Fin',108,1700),(110,'Acc',205,1700),(120,'Tre',NULL,1700),(130,'Cor',NULL,1700),(140,'Con',NULL,1700),(150,'Sha',NULL,1700),(160,'Ben',NULL,1700),(170,'Man',NULL,1700),(180,'Con',NULL,1700),(190,'Con',NULL,1700),(200,'Ope',NULL,1700),(210,'IT ',NULL,1700),(220,'NOC',NULL,1700),(230,'IT ',NULL,1700),(240,'Gov',NULL,1700),(250,'Ret',NULL,1700),(260,'Rec',NULL,1700),(270,'Pay',NULL,1700); /*Table structure for table `employees` */ DROP TABLE IF EXISTS `employees`; CREATE TABLE `employees` ( `employee_id` int(6) NOT NULL AUTO_INCREMENT, `first_name` varchar(20) DEFAULT NULL, `last_name` varchar(25) DEFAULT NULL, `email` varchar(25) DEFAULT NULL, `phone_number` varchar(20) DEFAULT NULL, `job_id` varchar(10) DEFAULT NULL, `salary` double(10,2) DEFAULT NULL, `commission_pct` double(4,2) DEFAULT NULL, `manager_id` int(6) DEFAULT NULL, `department_id` int(4) DEFAULT NULL, `hiredate` datetime DEFAULT NULL, PRIMARY KEY (`employee_id`), KEY `dept_id_fk` (`department_id`), KEY `job_id_fk` (`job_id`), CONSTRAINT `dept_id_fk` FOREIGN KEY (`department_id`) REFERENCES `departments` (`department_id`), CONSTRAINT `job_id_fk` FOREIGN KEY (`job_id`) REFERENCES `jobs` (`job_id`) ) ENGINE=InnoDB AUTO_INCREMENT=207 DEFAULT CHARSET=gb2312; /*Data for the table `employees` */ insert into `employees`(`employee_id`,`first_name`,`last_name`,`email`,`phone_number`,`job_id`,`salary`,`commission_pct`,`manager_id`,`department_id`,`hiredate`) values (100,'Steven','K_ing','SKING','515.123.4567','AD_PRES',24000.00,NULL,NULL,90,'1992-04-03 00:00:00'),(101,'Neena','Kochhar','NKOCHHAR','515.123.4568','AD_VP',17000.00,NULL,100,90,'1992-04-03 00:00:00'),(102,'Lex','De Haan','LDEHAAN','515.123.4569','AD_VP',17000.00,NULL,100,90,'1992-04-03 00:00:00'),(103,'Alexander','Hunold','AHUNOLD','590.423.4567','IT_PROG',9000.00,NULL,102,60,'1992-04-03 00:00:00'),(104,'Bruce','Ernst','BERNST','590.423.4568','IT_PROG',6000.00,NULL,103,60,'1992-04-03 00:00:00'),(105,'David','Austin','DAUSTIN','590.423.4569','IT_PROG',4800.00,NULL,103,60,'1998-03-03 00:00:00'),(106,'Valli','Pataballa','VPATABAL','590.423.4560','IT_PROG',4800.00,NULL,103,60,'1998-03-03 00:00:00'),(107,'Diana','Lorentz','DLORENTZ','590.423.5567','IT_PROG',4200.00,NULL,103,60,'1998-03-03 00:00:00'),(108,'Nancy','Greenberg','NGREENBE','515.124.4569','FI_MGR',12000.00,NULL,101,100,'1998-03-03 00:00:00'),(109,'Daniel','Faviet','DFAVIET','515.124.4169','FI_ACCOUNT',9000.00,NULL,108,100,'1998-03-03 00:00:00'),(110,'John','Chen','JCHEN','515.124.4269','FI_ACCOUNT',8200.00,NULL,108,100,'2000-09-09 00:00:00'),(111,'Ismael','Sciarra','ISCIARRA','515.124.4369','FI_ACCOUNT',7700.00,NULL,108,100,'2000-09-09 00:00:00'),(112,'Jose Manuel','Urman','JMURMAN','515.124.4469','FI_ACCOUNT',7800.00,NULL,108,100,'2000-09-09 00:00:00'),(113,'Luis','Popp','LPOPP','515.124.4567','FI_ACCOUNT',6900.00,NULL,108,100,'2000-09-09 00:00:00'),(114,'Den','Raphaely','DRAPHEAL','515.127.4561','PU_MAN',11000.00,NULL,100,30,'2000-09-09 00:00:00'),(115,'Alexander','Khoo','AKHOO','515.127.4562','PU_CLERK',3100.00,NULL,114,30,'2000-09-09 00:00:00'),(116,'Shelli','Baida','SBAIDA','515.127.4563','PU_CLERK',2900.00,NULL,114,30,'2000-09-09 00:00:00'),(117,'Sigal','Tobias','STOBIAS','515.127.4564','PU_CLERK',2800.00,NULL,114,30,'2000-09-09 00:00:00'),(118,'Guy','Himuro','GHIMURO','515.127.4565','PU_CLERK',2600.00,NULL,114,30,'2000-09-09 00:00:00'),(119,'Karen','Colmenares','KCOLMENA','515.127.4566','PU_CLERK',2500.00,NULL,114,30,'2000-09-09 00:00:00'),(120,'Matthew','Weiss','MWEISS','650.123.1234','ST_MAN',8000.00,NULL,100,50,'2004-02-06 00:00:00'),(121,'Adam','Fripp','AFRIPP','650.123.2234','ST_MAN',8200.00,NULL,100,50,'2004-02-06 00:00:00'),(122,'Payam','Kaufling','PKAUFLIN','650.123.3234','ST_MAN',7900.00,NULL,100,50,'2004-02-06 00:00:00'),(123,'Shanta','Vollman','SVOLLMAN','650.123.4234','ST_MAN',6500.00,NULL,100,50,'2004-02-06 00:00:00'),(124,'Kevin','Mourgos','KMOURGOS','650.123.5234','ST_MAN',5800.00,NULL,100,50,'2004-02-06 00:00:00'),(125,'Julia','Nayer','JNAYER','650.124.1214','ST_CLERK',3200.00,NULL,120,50,'2004-02-06 00:00:00'),(126,'Irene','Mikkilineni','IMIKKILI','650.124.1224','ST_CLERK',2700.00,NULL,120,50,'2004-02-06 00:00:00'),(127,'James','Landry','JLANDRY','650.124.1334','ST_CLERK',2400.00,NULL,120,50,'2004-02-06 00:00:00'),(128,'Steven','Markle','SMARKLE','650.124.1434','ST_CLERK',2200.00,NULL,120,50,'2004-02-06 00:00:00'),(129,'Laura','Bissot','LBISSOT','650.124.5234','ST_CLERK',3300.00,NULL,121,50,'2004-02-06 00:00:00'),(130,'Mozhe','Atkinson','MATKINSO','650.124.6234','ST_CLERK',2800.00,NULL,121,50,'2004-02-06 00:00:00'),(131,'James','Marlow','JAMRLOW','650.124.7234','ST_CLERK',2500.00,NULL,121,50,'2004-02-06 00:00:00'),(132,'TJ','Olson','TJOLSON','650.124.8234','ST_CLERK',2100.00,NULL,121,50,'2004-02-06 00:00:00'),(133,'Jason','Mallin','JMALLIN','650.127.1934','ST_CLERK',3300.00,NULL,122,50,'2004-02-06 00:00:00'),(134,'Michael','Rogers','MROGERS','650.127.1834','ST_CLERK',2900.00,NULL,122,50,'2002-12-23 00:00:00'),(135,'Ki','Gee','KGEE','650.127.1734','ST_CLERK',2400.00,NULL,122,50,'2002-12-23 00:00:00'),(136,'Hazel','Philtanker','HPHILTAN','650.127.1634','ST_CLERK',2200.00,NULL,122,50,'2002-12-23 00:00:00'),(137,'Renske','Ladwig','RLADWIG','650.121.1234','ST_CLERK',3600.00,NULL,123,50,'2002-12-23 00:00:00'),(138,'Stephen','Stiles','SSTILES','650.121.2034','ST_CLERK',3200.00,NULL,123,50,'2002-12-23 00:00:00'),(139,'John','Seo','JSEO','650.121.2019','ST_CLERK',2700.00,NULL,123,50,'2002-12-23 00:00:00'),(140,'Joshua','Patel','JPATEL','650.121.1834','ST_CLERK',2500.00,NULL,123,50,'2002-12-23 00:00:00'),(141,'Trenna','Rajs','TRAJS','650.121.8009','ST_CLERK',3500.00,NULL,124,50,'2002-12-23 00:00:00'),(142,'Curtis','Davies','CDAVIES','650.121.2994','ST_CLERK',3100.00,NULL,124,50,'2002-12-23 00:00:00'),(143,'Randall','Matos','RMATOS','650.121.2874','ST_CLERK',2600.00,NULL,124,50,'2002-12-23 00:00:00'),(144,'Peter','Vargas','PVARGAS','650.121.2004','ST_CLERK',2500.00,NULL,124,50,'2002-12-23 00:00:00'),(145,'John','Russell','JRUSSEL','011.44.1344.429268','SA_MAN',14000.00,0.40,100,80,'2002-12-23 00:00:00'),(146,'Karen','Partners','KPARTNER','011.44.1344.467268','SA_MAN',13500.00,0.30,100,80,'2002-12-23 00:00:00'),(147,'Alberto','Errazuriz','AERRAZUR','011.44.1344.429278','SA_MAN',12000.00,0.30,100,80,'2002-12-23 00:00:00'),(148,'Gerald','Cambrault','GCAMBRAU','011.44.1344.619268','SA_MAN',11000.00,0.30,100,80,'2002-12-23 00:00:00'),(149,'Eleni','Zlotkey','EZLOTKEY','011.44.1344.429018','SA_MAN',10500.00,0.20,100,80,'2002-12-23 00:00:00'),(150,'Peter','Tucker','PTUCKER','011.44.1344.129268','SA_REP',10000.00,0.30,145,80,'2014-03-05 00:00:00'),(151,'David','Bernstein','DBERNSTE','011.44.1344.345268','SA_REP',9500.00,0.25,145,80,'2014-03-05 00:00:00'),(152,'Peter','Hall','PHALL','011.44.1344.478968','SA_REP',9000.00,0.25,145,80,'2014-03-05 00:00:00'),(153,'Christopher','Olsen','COLSEN','011.44.1344.498718','SA_REP',8000.00,0.20,145,80,'2014-03-05 00:00:00'),(154,'Nanette','Cambrault','NCAMBRAU','011.44.1344.987668','SA_REP',7500.00,0.20,145,80,'2014-03-05 00:00:00'),(155,'Oliver','Tuvault','OTUVAULT','011.44.1344.486508','SA_REP',7000.00,0.15,145,80,'2014-03-05 00:00:00'),(156,'Janette','K_ing','JKING','011.44.1345.429268','SA_REP',10000.00,0.35,146,80,'2014-03-05 00:00:00'),(157,'Patrick','Sully','PSULLY','011.44.1345.929268','SA_REP',9500.00,0.35,146,80,'2014-03-05 00:00:00'),(158,'Allan','McEwen','AMCEWEN','011.44.1345.829268','SA_REP',9000.00,0.35,146,80,'2014-03-05 00:00:00'),(159,'Lindsey','Smith','LSMITH','011.44.1345.729268','SA_REP',8000.00,0.30,146,80,'2014-03-05 00:00:00'),(160,'Louise','Doran','LDORAN','011.44.1345.629268','SA_REP',7500.00,0.30,146,80,'2014-03-05 00:00:00'),(161,'Sarath','Sewall','SSEWALL','011.44.1345.529268','SA_REP',7000.00,0.25,146,80,'2014-03-05 00:00:00'),(162,'Clara','Vishney','CVISHNEY','011.44.1346.129268','SA_REP',10500.00,0.25,147,80,'2014-03-05 00:00:00'),(163,'Danielle','Greene','DGREENE','011.44.1346.229268','SA_REP',9500.00,0.15,147,80,'2014-03-05 00:00:00'),(164,'Mattea','Marvins','MMARVINS','011.44.1346.329268','SA_REP',7200.00,0.10,147,80,'2014-03-05 00:00:00'),(165,'David','Lee','DLEE','011.44.1346.529268','SA_REP',6800.00,0.10,147,80,'2014-03-05 00:00:00'),(166,'Sundar','Ande','SANDE','011.44.1346.629268','SA_REP',6400.00,0.10,147,80,'2014-03-05 00:00:00'),(167,'Amit','Banda','ABANDA','011.44.1346.729268','SA_REP',6200.00,0.10,147,80,'2014-03-05 00:00:00'),(168,'Lisa','Ozer','LOZER','011.44.1343.929268','SA_REP',11500.00,0.25,148,80,'2014-03-05 00:00:00'),(169,'Harrison','Bloom','HBLOOM','011.44.1343.829268','SA_REP',10000.00,0.20,148,80,'2014-03-05 00:00:00'),(170,'Tayler','Fox','TFOX','011.44.1343.729268','SA_REP',9600.00,0.20,148,80,'2014-03-05 00:00:00'),(171,'William','Smith','WSMITH','011.44.1343.629268','SA_REP',7400.00,0.15,148,80,'2014-03-05 00:00:00'),(172,'Elizabeth','Bates','EBATES','011.44.1343.529268','SA_REP',7300.00,0.15,148,80,'2014-03-05 00:00:00'),(173,'Sundita','Kumar','SKUMAR','011.44.1343.329268','SA_REP',6100.00,0.10,148,80,'2014-03-05 00:00:00'),(174,'Ellen','Abel','EABEL','011.44.1644.429267','SA_REP',11000.00,0.30,149,80,'2014-03-05 00:00:00'),(175,'Alyssa','Hutton','AHUTTON','011.44.1644.429266','SA_REP',8800.00,0.25,149,80,'2014-03-05 00:00:00'),(176,'Jonathon','Taylor','JTAYLOR','011.44.1644.429265','SA_REP',8600.00,0.20,149,80,'2014-03-05 00:00:00'),(177,'Jack','Livingston','JLIVINGS','011.44.1644.429264','SA_REP',8400.00,0.20,149,80,'2014-03-05 00:00:00'),(178,'Kimberely','Grant','KGRANT','011.44.1644.429263','SA_REP',7000.00,0.15,149,NULL,'2014-03-05 00:00:00'),(179,'Charles','Johnson','CJOHNSON','011.44.1644.429262','SA_REP',6200.00,0.10,149,80,'2014-03-05 00:00:00'),(180,'Winston','Taylor','WTAYLOR','650.507.9876','SH_CLERK',3200.00,NULL,120,50,'2014-03-05 00:00:00'),(181,'Jean','Fleaur','JFLEAUR','650.507.9877','SH_CLERK',3100.00,NULL,120,50,'2014-03-05 00:00:00'),(182,'Martha','Sullivan','MSULLIVA','650.507.9878','SH_CLERK',2500.00,NULL,120,50,'2014-03-05 00:00:00'),(183,'Girard','Geoni','GGEONI','650.507.9879','SH_CLERK',2800.00,NULL,120,50,'2014-03-05 00:00:00'),(184,'Nandita','Sarchand','NSARCHAN','650.509.1876','SH_CLERK',4200.00,NULL,121,50,'2014-03-05 00:00:00'),(185,'Alexis','Bull','ABULL','650.509.2876','SH_CLERK',4100.00,NULL,121,50,'2014-03-05 00:00:00'),(186,'Julia','Dellinger','JDELLING','650.509.3876','SH_CLERK',3400.00,NULL,121,50,'2014-03-05 00:00:00'),(187,'Anthony','Cabrio','ACABRIO','650.509.4876','SH_CLERK',3000.00,NULL,121,50,'2014-03-05 00:00:00'),(188,'Kelly','Chung','KCHUNG','650.505.1876','SH_CLERK',3800.00,NULL,122,50,'2014-03-05 00:00:00'),(189,'Jennifer','Dilly','JDILLY','650.505.2876','SH_CLERK',3600.00,NULL,122,50,'2014-03-05 00:00:00'),(190,'Timothy','Gates','TGATES','650.505.3876','SH_CLERK',2900.00,NULL,122,50,'2014-03-05 00:00:00'),(191,'Randall','Perkins','RPERKINS','650.505.4876','SH_CLERK',2500.00,NULL,122,50,'2014-03-05 00:00:00'),(192,'Sarah','Bell','SBELL','650.501.1876','SH_CLERK',4000.00,NULL,123,50,'2014-03-05 00:00:00'),(193,'Britney','Everett','BEVERETT','650.501.2876','SH_CLERK',3900.00,NULL,123,50,'2014-03-05 00:00:00'),(194,'Samuel','McCain','SMCCAIN','650.501.3876','SH_CLERK',3200.00,NULL,123,50,'2014-03-05 00:00:00'),(195,'Vance','Jones','VJONES','650.501.4876','SH_CLERK',2800.00,NULL,123,50,'2014-03-05 00:00:00'),(196,'Alana','Walsh','AWALSH','650.507.9811','SH_CLERK',3100.00,NULL,124,50,'2014-03-05 00:00:00'),(197,'Kevin','Feeney','KFEENEY','650.507.9822','SH_CLERK',3000.00,NULL,124,50,'2014-03-05 00:00:00'),(198,'Donald','OConnell','DOCONNEL','650.507.9833','SH_CLERK',2600.00,NULL,124,50,'2014-03-05 00:00:00'),(199,'Douglas','Grant','DGRANT','650.507.9844','SH_CLERK',2600.00,NULL,124,50,'2014-03-05 00:00:00'),(200,'Jennifer','Whalen','JWHALEN','515.123.4444','AD_ASST',4400.00,NULL,101,10,'2016-03-03 00:00:00'),(201,'Michael','Hartstein','MHARTSTE','515.123.5555','MK_MAN',13000.00,NULL,100,20,'2016-03-03 00:00:00'),(202,'Pat','Fay','PFAY','603.123.6666','MK_REP',6000.00,NULL,201,20,'2016-03-03 00:00:00'),(203,'Susan','Mavris','SMAVRIS','515.123.7777','HR_REP',6500.00,NULL,101,40,'2016-03-03 00:00:00'),(204,'Hermann','Baer','HBAER','515.123.8888','PR_REP',10000.00,NULL,101,70,'2016-03-03 00:00:00'),(205,'Shelley','Higgins','SHIGGINS','515.123.8080','AC_MGR',12000.00,NULL,101,110,'2016-03-03 00:00:00'),(206,'William','Gietz','WGIETZ','515.123.8181','AC_ACCOUNT',8300.00,NULL,205,110,'2016-03-03 00:00:00'); /*Table structure for table `jobs` */ DROP TABLE IF EXISTS `jobs`; CREATE TABLE `jobs` ( `job_id` varchar(10) NOT NULL, `job_title` varchar(35) DEFAULT NULL, `min_salary` int(6) DEFAULT NULL, `max_salary` int(6) DEFAULT NULL, PRIMARY KEY (`job_id`) ) ENGINE=InnoDB DEFAULT CHARSET=gb2312; /*Data for the table `jobs` */ insert into `jobs`(`job_id`,`job_title`,`min_salary`,`max_salary`) values ('AC_ACCOUNT','Public Accountant',4200,9000),('AC_MGR','Accounting Manager',8200,16000),('AD_ASST','Administration Assistant',3000,6000),('AD_PRES','President',20000,40000),('AD_VP','Administration Vice President',15000,30000),('FI_ACCOUNT','Accountant',4200,9000),('FI_MGR','Finance Manager',8200,16000),('HR_REP','Human Resources Representative',4000,9000),('IT_PROG','Programmer',4000,10000),('MK_MAN','Marketing Manager',9000,15000),('MK_REP','Marketing Representative',4000,9000),('PR_REP','Public Relations Representative',4500,10500),('PU_CLERK','Purchasing Clerk',2500,5500),('PU_MAN','Purchasing Manager',8000,15000),('SA_MAN','Sales Manager',10000,20000),('SA_REP','Sales Representative',6000,12000),('SH_CLERK','Shipping Clerk',2500,5500),('ST_CLERK','Stock Clerk',2000,5000),('ST_MAN','Stock Manager',5500,8500); /*Table structure for table `locations` */ DROP TABLE IF EXISTS `locations`; CREATE TABLE `locations` ( `location_id` int(11) NOT NULL AUTO_INCREMENT, `street_address` varchar(40) DEFAULT NULL, `postal_code` varchar(12) DEFAULT NULL, `city` varchar(30) DEFAULT NULL, `state_province` varchar(25) DEFAULT NULL, `country_id` varchar(2) DEFAULT NULL, PRIMARY KEY (`location_id`) ) ENGINE=InnoDB AUTO_INCREMENT=3201 DEFAULT CHARSET=gb2312; /*Data for the table `locations` */ insert into `locations`(`location_id`,`street_address`,`postal_code`,`city`,`state_province`,`country_id`) values (1000,'1297 Via Cola di Rie','00989','Roma',NULL,'IT'),(1100,'93091 Calle della Testa','10934','Venice',NULL,'IT'),(1200,'2017 Shinjuku-ku','1689','Tokyo','Tokyo Prefecture','JP'),(1300,'9450 Kamiya-cho','6823','Hiroshima',NULL,'JP'),(1400,'2014 Jabberwocky Rd','26192','Southlake','Texas','US'),(1500,'2011 Interiors Blvd','99236','South San Francisco','California','US'),(1600,'2007 Zagora St','50090','South Brunswick','New Jersey','US'),(1700,'2004 Charade Rd','98199','Seattle','Washington','US'),(1800,'147 Spadina Ave','M5V 2L7','Toronto','Ontario','CA'),(1900,'6092 Boxwood St','YSW 9T2','Whitehorse','Yukon','CA'),(2000,'40-5-12 Laogianggen','190518','Beijing',NULL,'CN'),(2100,'1298 Vileparle (E)','490231','Bombay','Maharashtra','IN'),(2200,'12-98 Victoria Street','2901','Sydney','New South Wales','AU'),(2300,'198 Clementi North','540198','Singapore',NULL,'SG'),(2400,'8204 Arthur St',NULL,'London',NULL,'UK'),(2500,'Magdalen Centre, The Oxford Science Park','OX9 9ZB','Oxford','Oxford','UK'),(2600,'9702 Chester Road','09629850293','Stretford','Manchester','UK'),(2700,'Schwanthalerstr. 7031','80925','Munich','Bavaria','DE'),(2800,'Rua Frei Caneca 1360 ','01307-002','Sao Paulo','Sao Paulo','BR'),(2900,'20 Rue des Corps-Saints','1730','Geneva','Geneve','CH'),(3000,'Murtenstrasse 921','3095','Bern','BE','CH'),(3100,'Pieter Breughelstraat 837','3029SK','Utrecht','Utrecht','NL'),(3200,'Mariano Escobedo 9991','11932','Mexico City','Distrito Federal,','MX'); /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
1 #一、按关系表达式筛选 2 #案例1:查询部门编号不是100的员工信息 3 SELECT * FROM employees WHERE department_id <> 100; 4 5 #案例2:查询工资<15000的姓名、工资 6 SELECT last_name,salary FROM employees WHERE salary<15000; 7 8 #二、按逻辑表达式筛选 9 10 #案例1:查询部门编号不是 50-100之间员工姓名、部门编号、邮箱 11 #方式1: 12 SELECT last_name,department_id,email FROM employees WHERE department_id <50 OR department_id>100; 13 14 #方式2: 15 SELECT last_name,department_id,email FROM employees WHERE NOT(department_id>=50 AND department_id<=100); 16 17 #案例2:查询奖金率>0.03 或者 员工编号在60-110之间的员工信息 18 SELECT * FROM employees WHERE commission_pct>0.03 OR (employee_id >=60 AND employee_id<=110); 19 20 #三、模糊查询 21 #1、like 22 /* 23 功能:一般和通配符搭配使用,对字符型数据进行部分匹配查询 24 常见的通配符: 25 _ 任意单个字符 26 % 任意多个字符,支持0-多个 27 like/not like 28 */ 29 #案例1:查询姓名中包含字符a的员工信息 30 SELECT * FROM employees WHERE last_name LIKE '%a%'; 31 32 #案例2:查询姓名中包含最后一个字符为e的员工信息 33 SELECT * FROM employees WHERE last_name LIKE '%e'; 34 35 #案例3:查询姓名中包含第一个字符为e的员工信息 36 SELECT * FROM employees WHERE last_name LIKE 'e%'; 37 38 #案例4:查询姓名中包含第三个字符为x的员工信息 39 SELECT * FROM employees WHERE last_name LIKE '__x%'; 40 41 #案例5:查询姓名中包含第二个字符为_的员工信息 42 SELECT * FROM employees WHERE last_name LIKE '_\_%'; 43 44 SELECT * FROM employees WHERE last_name LIKE '_$_%' ESCAPE '$'; 45 46 #2、in 47 /* 48 功能:查询某字段的值是否属于指定的列表之内 49 50 a in(常量值1,常量值2,常量值3,...) 51 a not in(常量值1,常量值2,常量值3,...) 52 53 in/not in 54 */ 55 56 #案例1:查询部门编号是30/50/90的员工名、部门编号 57 58 #方式1: 59 SELECT last_name,department_id FROM employees WHERE department_id IN(30,50,90); 60 61 #方式2: 62 SELECT last_name,department_id 63 FROM employees 64 WHERE department_id = 30 65 OR department_id = 50 66 OR department_id = 90; 67 68 #案例2:查询工种编号不是SH_CLERK或IT_PROG的员工信息 69 #方式1: 70 SELECT * FROM employees WHERE job_id NOT IN('SH_CLERK','IT_PROG'); 71 72 #方式2: 73 SELECT * FROM employees WHERE NOT(job_id ='SH_CLERK' OR job_id = 'IT_PROG'); 74 75 #3、between and 76 /* 77 功能:判断某个字段的值是否介于xx之间 78 79 between and/not between and 80 81 */ 82 #案例1:查询部门编号是30-90之间的部门编号、员工姓名 83 84 #方式1: 85 SELECT department_id,last_name FROM employees WHERE department_id BETWEEN 30 AND 90; 86 87 #方式2: 88 89 SELECT department_id,last_name FROM employees WHERE department_id>=30 AND department_id<=90; 90 91 #案例2:查询年薪不是100000-200000之间的员工姓名、工资、年薪 92 SELECT last_name,salary,salary*12*(1+IFNULL(commission_pct,0)) 年薪 93 FROM employees 94 WHERE salary*12*(1+IFNULL(commission_pct,0))<100000 OR salary*12*(1+IFNULL(commission_pct,0))>200000; 95 96 SELECT last_name,salary,salary*12*(1+IFNULL(commission_pct,0)) 年薪 97 FROM employees 98 WHERE salary*12*(1+IFNULL(commission_pct,0)) NOT BETWEEN 100000 AND 200000; 99 100 #4、is null/is not null 101 102 #案例1:查询没有奖金的员工信息 103 SELECT * FROM employees WHERE commission_pct IS NULL; 104 105 #案例2:查询有奖金的员工信息 106 SELECT * FROM employees WHERE commission_pct IS NOT NULL; 107 108 SELECT * FROM employees WHERE salary IS 10000; 109 110 #----------------对比------------------------------------ 111 112 = 只能判断普通的内容 113 114 IS 只能判断NULL值 115 116 <=> 安全等于,既能判断普通内容,又能判断NULL值 117 118 SELECT * FROM employees WHERE salary <=> 10000; 119 120 SELECT * FROM employees WHERE commission_