#连接查询
概念:查询的字段来自于多个表
语法:
select字段1,字段2
from表1,表2
【where连接条件】
笛卡尔乘积:
产生原因:没有加连接条件,导致结果为表1的行数*表2 的行数
解决方法:添加上有效的`myemployees`
分类:
一、传统模式的多表连接
等值连接(where)——非等值连接
二、sql99推出的标准,使用join关键字实现连接
内连接——外连接
三、自连接
#一、传统模式的多表连接
#也叫等值连接
特点有
①表的顺序没有要求
②n表连接,至少需要n-1个连接条件
③一般需要为表起别名,这样可以提高语句简洁度,并且防止字段有歧义
④可以添加分组、排序、筛选,一起混合使用
多表查询
#①案例1:查询员工名、部门名
SELECT employees.last_name,departments.department_id
from employees,departments
WHERE employees.department_id = departments.department_id
#②为表起别名
SELECT e.last_name,d.department_id
from employees e,departments d
WHERE e.department_id = d.department_id
SELECT e.last_name,d.department_name
FROM employees e,departments d
WHERE e.department_id=d.department_id;
#③添加筛选条件
#案例:查询 工资>5000的工种名和员工名、工资
SELECT job_title,last_name,salary
from employees e,jobs j
WHERE e.job_id = j.job_id and salary > 5000
order by salary desc
#④添加分组和筛选
#01案例:查询每个部门的员工个数和部门名
SELECT count(*),department_name
from employees e,departments d
WHERE e.department_id = d.department_id
GROUP BY e.department_id
#⑤排序
#01案例:查询每个部门的拥有5个员工以上员工个数和部门名,且逆序
SELECT count(*),department_name
from employees e,departments d
WHERE e.department_id = d.department_id
GROUP BY e.department_id
HAVING count(*) > 5
ORDER BY count(*) DESC
#⑥ 三表连接
#案例:查询员工名、部门名和所在城市
SELECT e.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
#1.显示所有员工的姓名,部门号和部门名称。
SELECT e.last_name,e.department_id,d.department_name
from employees e,departments d
WHERE e.department_id = d.department_id
#2.查询90号部门员工的job_id和90号部门的location_id
SELECT e.job_id,location_id,e.department_id
from employees e,departments d
WHERE e.department_id = d.department_id
and e.department_id = 90
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);
#2.非等值连接
#案例1:查询员工的工资以及对应的工资级别
SELECT salary,grade_level
FROM
employees e,
job_grades j
WHERE
e.salary BETWEEN j.lowest_sal
AND j.highest_sal
SELECT salary,grade_level
FROM employees e,job_grades g
WHERE e.`salary` BETWEEN g.`lowest_sal` AND g.`highest_sal`;
#案例1 :查询员工名、部门名
select last_name,department_name from employees e
inner join departments d on e.department_id = d.department_id
#案例2:查询有奖金的员工名、部门名
select last_name,department_name from employees e
inner join departments d on e.department_id = d.department_id
where commission_pct is not null
#案例3:查询城市名、员工名和部门名
SELECT city,last_name,department_name
FROM employees e
inner join departments d on e.department_id = d.department_id
inner join locations l on l.location_id = d.location_id
#案例1:查询没有男朋友的女神名称
use girls
select b.name from beauty b
left outer join boys o on b.boyfriend_id = o.id
#案例2:查询哪个城市没有部门
select l.city from locations l
left outer join departments d on l.location_id = d.location_id
where d.department_id is null
#三、自连接
#案例:查询员工名和上级领导的名字
select e.last_name,m.manager_id from employees e
inner join employees m on e.employee_id = m.manager_id
where m.manager_id is not null
#三、查询部门名为SAL或IT的员工信息
select e.*,d.department_name,d.department_id
from employees e
left join departments d on e.department_id = d.department_id
where d.department_name in(SAL,IT)
SELECT e.*,d.department_name,d.department_id
FROM employees e
RIGHT JOIN departments d ON e.`department_id`=d.`department_id`
WHERE d.`department_name` IN('SAL','IT');
#四、选择指定员工的姓名,员工号,以及他的管理者的姓名和员工号,结果类似于下面的格式
/*
employees Emp# manager Mgr#
kochhar 101 king 100
*/
SELECT e.last_name employees,
e.`employee_id` "Emp#",
m.last_name manager,
m.employee_id "Mgr#"
FROM employees e
INNER JOIN employees m ON e.`manager_id`=m.employee_id
WHERE e.`last_name`='kochhar';