#进阶6 连接查询
/*
含义:又称多表查询,当查询字段来自多个表时,就会用到连接查询
笛卡尔乘积:表1有m行,表2有n行,结果=m*n行
发生原因:没有有效的连接条件
如何避免:添加有效的连接条件
分类:
按年代分:
sql92标准:仅仅支持内连接
sql99标准【推荐】:支持内连接、外连接(左外和右外)、交叉连接
按功能分类:
内连接:
等值连接
非等值连接
自连接
外连接:
左外连接
右外连接
全外连接
交叉连接
*/
SELECT * FROM beauty;
SELECT NAME,boyname FROM boys,beauty; #笛卡尔乘积现象
#一、sql92标准
#1.等值连接
/*
1.多表等值连接的结果为多表的交集部分
2.n表连接,至少需要n-1个连接条件
3.多表的顺序没有要求
4.一般需要为表起别名
5.可以搭配前面的查询子句使用,比如排序、分组,筛选
*/
#案例1:查询女神名和对应的男神名
SELECT NAME,boyname
FROM boys,beauty
WHERE beauty.`boyfriend_id`=boys.`id`;
#案例2:查询员工名和对应的部门名
SELECT `last_name`,`department_name`
FROM `employees`,`departments`
WHERE employees.`department_id`=departments.`department_id`;
#2、为表起别名
/*
1.提高语句简洁度
2.区分多个重名字段
注意:如果为表起了别名,则查询字段就不能使用原来的表名限定
*/
#查询员工名,工种号,工种名
SELECT e.last_name,e.job_id,j.job_title
FROM employees e,jobs j
WHERE e.`job_id`=j.`job_id`;
#3.两个表的顺序可以调换
SELECT e.last_name,e.job_id,j.job_title
FROM jobs j,employees e
WHERE e.`job_id`=j.`job_id`;
#4.可以加筛选
#查询有奖金的员工名,部门名
SELECT first_name,`department_name`,`commission_pct`
FROM employees e,departments d
WHERE e.`department_id`=d.`department_id`
AND `commission_pct` IS NOT NULL;
#查询城市名中第二个字符为o的部门名和城市名
SELECT department_name,city
FROM departments d,locations l
WHERE d.`location_id`=l.`location_id`
AND city LIKE '_o%';
#5.可以加分组
#案例1:查询每个城市的部门个数
SELECT COUNT(*) 个数,city
FROM departments d,locations l
WHERE d.`location_id` = l.`location_id`
GROUP BY city;
#案例2:查询有奖金的每个部门名和部门领导者编号和该部门的最低工资
SELECT d.department_name,d.`manager_id`,MIN(salary)
FROM employees e,departments d
WHERE e.`department_id`=d.`department_id`
AND commission_pct IS NOT NULL
GROUP BY d.`department_name`,d.`department_id`;
#6.可以加排序
#案例:查询每个工种的工种名和员工个数,并按员工个数排序
SELECT job_title,COUNT(*)
FROM employees e,jobs j
WHERE e.`job_id` = j.`job_id`
GROUP BY job_title
ORDER BY COUNT(*) DESC;
#7.可以实现3表连接
#案例:查询员工名,部门名,所在城市
SELECT last_name,department_name,city
FROM employees e,departments d,locations l
WHERE e.`department_id`=d.`department_id`
AND d.`location_id`=l.`location_id`;
2.非等值连接
#案例1:查询员工的工资和工资级别
#创建工资等级表
CREATE TABLE job_grades
(grade_level VARCHAR(3),
lowest_sal INT,
highest_sal INT);
INSERT INTO job_grades
VALUES ('A', 1000, 2999);
INSERT INTO job_grades
VALUES ('B', 3000, 5999);
INSERT INTO job_grades
VALUES('C', 6000, 9999);
INSERT INTO job_grades
VALUES('D', 10000, 14999);
INSERT INTO job_grades
VALUES('E', 15000, 24999);
INSERT INTO job_grades
VALUES('F', 25000, 40000);
#查看等级
SELECT * FROM `job_grades`;
#查看工资对应等级
SELECT salary,grade_level
FROM employees e,job_grades g
WHERE salary BETWEEN g.`lowest_sal` AND g.`highest_sal`;
#查看等于A的工资级别
SELECT salary,grade_level
FROM employees e,job_grades g
WHERE salary BETWEEN g.`lowest_sal` AND g.`highest_sal`
AND `grade_level`='A';
#3.自连接
#案例:查询员工名和上级的名称
SELECT e.employee_id,e.last_name,m.employee_id,m.last_name
FROM employees e,employees m
WHERE e.`manager_id`=m.`employee_id`;
#二、sql99标准
/*
语法:
select 查询列表
from 表1 别名 【连接类型】
join 表2 别名
on 连接条件
【where 筛选条件】
【group by 分组】
【having 筛选条件】
【order by 排序列表】
连接类型
内连接:inner
外连接:
左外:left【outer】
右外:right【outer】
全外:full【outer】
交叉连接:cross
*/
#一、内连接
/*
语法:
select 查询列表
from 表1 别名
inner join 表2 别名
on 连接条件;
分类:
等值
非等值
自连接
特点:
1.可以添加排序、分组、筛选
2.inner可以省略
3.筛选条件放在where后,连接条件放在on后面,提高分离性,便于阅读
4.inner join连接和sql92语法中的等值连接效果相同,都是查询多表交集
*/
#1.等值连接
#案例1.查询员工名、部门名
SELECT last_name,department_name
FROM employees e
INNER JOIN departments d
ON e.`department_id`=d.`department_id`;
#案例2.查询名字中包含e的员工名和工种名(筛选)
SELECT last_name,job_title
FROM employees e
INNER JOIN jobs j
ON e.`job_id`=j.`job_id`
WHERE e.`last_name` LIKE '%e%';
#案例3.查询部门个数>3的城市名和部门个数(分组+筛选)
SELECT city,COUNT(*) 部门个数
FROM departments d
INNER JOIN locations l
ON d.`location_id`=l.`location_id`
GROUP BY city
HAVING COUNT(*)>3;
#案例4.查询哪个部门的员工个数>3的部门名和员工个数,并按个数降序(排序)
SELECT department_name,COUNT(*)
FROM departments d
INNER JOIN employees e
ON d.`department_id`=e.`department_id`
GROUP BY `department_name`
HAVING COUNT(*)>3
ORDER BY COUNT(*) DESC;
#案例5.查询员工名,部门名,工种名,并按部门名降序
SELECT last_name,department_name,job_title
FROM employees e
INNER JOIN departments d ON e.department_id = d.department_id
INNER JOIN jobs j ON e.`job_id` = j.`job_id`
ORDER BY department_name DESC;
#2.非等值连接
#查询员工工资级别
SELECT salary,grade_level
FROM employees e
INNER JOIN job_grades j
ON e.`salary` BETWEEN j.`lowest_sal` AND j.`highest_sal`;
#查询工资级别的个数>20的个数,并按工资级别降序
SELECT COUNT(*),grade_level
FROM employees e
JOIN job_grades j
ON e.`salary` BETWEEN `lowest_sal` AND `highest_sal`
GROUP BY grade_level
HAVING COUNT(*)>20
ORDER BY grade_level DESC;
#3.自连接
#案例:查询员工名和上级的名称
SELECT e.last_name,m.last_name
FROM employees e
JOIN employees m
ON e.`manager_id` = m.`employee_id`;
#查询字符中包含k员工名和上级的名称
SELECT e.last_name,m.last_name
FROM employees e
JOIN employees m
ON e.`manager_id` = m.`employee_id`
WHERE e.`last_name` LIKE '%k%';
#二、外连接
/*
应用场景:用于查询一个表中有,另一个表中没有的记录
特点:
1.外连接查询结果为主表中所有记录
①如果从表中有和它匹配的,则显示匹配的值
②如果从表中没有和它匹配的,则显示为null
③外连接查询结果=内连接结果+主表中有而从表中没有的记录
2.左外连接,left join左边的是主表
右外连接,right join右边的是主表
3.左外和右外交换两个表的顺序,可以实现同样的效果
4.全外连接=内连接结果+表1有但表2没有+表2有但表1没有
*/
#引入:查询男朋友不在男神表的女神名
#1.左外连接实现
SELECT b.name,bo.*
FROM beauty b
LEFT OUTER JOIN boys bo
ON b.`boyfriend_id` = bo.`id`
WHERE bo.`id` IS NULL;
#2.右外连接实现
SELECT b.name,bo.*
FROM boys bo
RIGHT OUTER JOIN beauty b
ON b.`boyfriend_id` = bo.`id`
WHERE bo.`id` IS NULL;
#案例1:查询哪个部门没有员工
#左外
SELECT d.*,e.employee_id
FROM departments d
LEFT OUTER JOIN employees e
ON d.`department_id` = e.`department_id`
WHERE e.employee_id IS NULL;
#右外
SELECT d.*,e.employee_id
FROM employees e
RIGHT OUTER JOIN departments d
ON d.`department_id` = e.`department_id`
WHERE e.employee_id IS NULL;
#全外
USE girls
SELECT b.name,bo.*
FROM beauty b
FULL OUTER JOIN boys bo
ON b.`boyfriend_id` = bo.`id`
#交叉连接(笛卡尔积)
USE girls;
SELECT b.*,bo.*
FROM beauty b
CROSS JOIN boys bo;
mysql——连接查询
最新推荐文章于 2024-08-16 14:54:20 发布