【MySQL】多表查询 综合练习

本文详细介绍了SQL中的多表查询实例,包括使用LEFTJOIN、INNERJOIN和RIGHTJOIN来获取员工的姓名、部门信息,以及特定条件下的部门和员工筛选。还涉及了如何创建和使用新表进行部门和员工的关联查询。
摘要由CSDN通过智能技术生成

多表查询笔记

综合练习

# 1.显示所有员工的姓名,部门号和部门名称。
SELECT e.last_name,e.department_id,d.department_name
FROM employees e 
LEFT JOIN departments d 
ON e.department_id = d.department_id;

SELECT e.last_name,e.department_id,d.department_name
FROM employees e 
LEFT JOIN departments d 
USING(department_id);

# 2.查询90号部门员工的job_id和90号部门的location_id
SELECT e.employee_id,e.job_id,d.department_id,d.location_id
FROM employees e,departments d 
WHERE e.department_id = d.department_id
AND e.department_id = 90;

SELECT e.employee_id,e.job_id,d.department_id,d.location_id
FROM employees e
JOIN departments d 
ON e.department_id = d.department_id
WHERE e.department_id = 90;

SELECT e.employee_id,e.job_id,d.department_id,d.location_id
FROM employees e
JOIN departments d 
USING(department_id)
WHERE e.department_id = 90;

# 3.选择所有有奖金的员工的 last_name , department_name , location_id , city
SELECT e.employee_id, e.last_name,d.department_name,l.location_id,l.city
FROM employees e 
JOIN departments d ON e.department_id = d.department_id 
JOIN locations l ON d.location_id = l.location_id
WHERE NOT ISNULL(e.commission_pct);

SELECT e.employee_id, e.last_name,d.department_name,l.location_id,l.city
FROM employees e 
JOIN departments d ON e.department_id = d.department_id 
JOIN locations l ON d.location_id = l.location_id
WHERE e.commission_pct IS NOT NULL;


# 4.选择city在Toronto工作的员工的 last_name , job_id , department_id , department_name
SELECT last_name , job_id , department_id , department_name
FROM employees e 
JOIN departments d 
USING(department_id)
JOIN locations l 
USING(location_id)
WHERE l.city = 'Toronto';


# 5.查询员工所在的部门名称、部门地址、姓名、工作、工资,其中员工所在部门的部门名称为’Executive’
SELECT e.employee_id,e.last_name,e.salary,e.job_id,d.department_name,l.street_address
FROM employees e 
JOIN departments d ON e.department_id = d.department_id 
JOIN locations l ON d.location_id = l.location_id
WHERE d.department_name = 'Executive';


# 6.选择指定员工的姓名,员工号,以及他的管理者的姓名和员工号,结果类似于下面的格式
# kochhar 101 king 100
SELECT e.last_name,e.employee_id,m.last_name,m.manager_id
FROM employees e , employees m
WHERE e.manager_id = m.employee_id;

SELECT CONCAT(e.last_name,'  ',e.employee_id,'  ',m.last_name,'  ',IFNULL(m.manager_id,100))
FROM employees e , employees m
WHERE e.manager_id = m.employee_id


# 7.查询哪些部门没有员工
SELECT e.employee_id,d.department_id,d.department_name
FROM employees e 
RIGHT JOIN departments d USING(department_id) -- 查询显示右表 --
WHERE e.employee_id IS NULL; -- 多虑左边 --


#方式1:
SELECT d.department_id
FROM departments d LEFT JOIN employees e
ON e.department_id = d.`department_id`
WHERE e.department_id IS NULL
#方式2:
SELECT department_id
FROM departments d
WHERE NOT EXISTS (
SELECT *
FROM employees e
WHERE e.`department_id` = d.`department_id`
)

# 8. 查询哪个城市没有部门
SELECT d.department_name,l.location_id,l.city
FROM departments d 
RIGHT JOIN locations l      --   查右 滤左     --
ON d.location_id = l.location_id
where d.department_id IS NULL;

SELECT l.location_id,l.city
FROM locations l LEFT JOIN departments d
ON l.`location_id` = d.`location_id`
WHERE d.`location_id` IS NULL


# 9. 查询部门名为 Sales 或 IT 的员工信息
SELECT e.employee_id,e.last_name,d.department_name
FROM employees e 
JOIN departments d 
ON e.department_id = d.department_id 
WHERE d.department_name = 'Sales'
OR d.department_name = 'IT';

SELECT employee_id,last_name,department_name
FROM employees e,departments d
WHERE e.department_id = d.`department_id`
AND d.`department_name` IN ('Sales','IT');

新建表2:

CREATE TABLE `t_dept` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`deptName` VARCHAR(30) DEFAULT NULL,
`address` VARCHAR(40) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

CREATE TABLE `t_emp` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(20) DEFAULT NULL,
`age` INT(3) DEFAULT NULL,
`deptId` INT(11) DEFAULT NULL,
empno int not null,
PRIMARY KEY (`id`),
KEY `idx_dept_id` (`deptId`)
#CONSTRAINT `fk_dept_id` FOREIGN KEY (`deptId`) REFERENCES `t_dept` (`id`)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

#1.所有有门派的人员信息 ( A、B两表共有)

INSERT INTO t_dept(deptName,address) VALUES('华山','华山');
INSERT INTO t_dept(deptName,address) VALUES('丐帮','洛阳');
INSERT INTO t_dept(deptName,address) VALUES('峨眉','峨眉山');
INSERT INTO t_dept(deptName,address) VALUES('武当','武当山');
INSERT INTO t_dept(deptName,address) VALUES('明教','光明顶');
INSERT INTO t_dept(deptName,address) VALUES('少林','少林寺');
INSERT INTO t_emp(NAME,age,deptId,empno) VALUES('风清扬',90,1,100001);
INSERT INTO t_emp(NAME,age,deptId,empno) VALUES('岳不群',50,1,100002);
INSERT INTO t_emp(NAME,age,deptId,empno) VALUES('令狐冲',24,1,100003);
INSERT INTO t_emp(NAME,age,deptId,empno) VALUES('洪七公',70,2,100004);
INSERT INTO t_emp(NAME,age,deptId,empno) VALUES('乔峰',35,2,100005);
INSERT INTO t_emp(NAME,age,deptId,empno) VALUES('灭绝师太',70,3,100006);
INSERT INTO t_emp(NAME,age,deptId,empno) VALUES('周芷若',20,3,100007);
INSERT INTO t_emp(NAME,age,deptId,empno) VALUES('张三丰',100,4,100008);
INSERT INTO t_emp(NAME,age,deptId,empno) VALUES('张无忌',25,5,100009);
INSERT INTO t_emp(NAME,age,deptId,empno) VALUES('韦小宝',18,null,100010);


#【题目】
#1.所有有门派的人员信息 ( A、B两表共有)
SELECT e.id,e.`name`,e.age,d.id AS "门派ID",d.deptName
FROM t_emp e 
JOIN t_dept d
ON e.deptId = d.id

#2.列出所有用户,并显示其机构信息 (A的全集)
SELECT e.id,e.`name`,e.age,d.id AS "门派ID",d.deptName
FROM t_emp e 
LEFT JOIN t_dept d
ON e.deptId = d.id

#3.列出所有门派 (B的全集)
SELECT *
FROM t_dept;

SELECT d.id AS "门派ID",d.deptName
FROM t_emp e 
RIGHT JOIN t_dept d
ON e.deptId = d.id;

#4.所有不入门派的人员 (A的独有)
# 公示: 查左 滤右
SELECT e.id,e.`name`,e.age,d.id AS "门派ID",d.deptName
FROM t_emp e 
LEFT JOIN t_dept d  
ON e.deptId = d.id
WHERE d.id IS NULL;

#5.所有没人入的门派 (B的独有)
# 公式: 查右 滤左
SELECT e.id,e.`name`,e.age,d.id AS "门派ID",d.deptName
FROM t_emp e 
RIGHT JOIN t_dept d
ON e.deptId = d.id
WHERE e.deptId IS NULL;


#6.列出所有人员和机构的对照关系 (AB全有)
# 左右外连接 使用 UNION 再连接
SELECT * FROM t_emp e LEFT JOIN t_dept d ON e.deptId = d.id
UNION
SELECT * FROM t_emp e RIGHT JOIN t_dept d ON e.deptId = d.id;

#MySQL Full Join的实现 因为MySQL不支持FULL JOIN,下面是替代方法
#left join + union(可去除重复数据)+ right join
#7.列出所有没入派的人员和没人入的门派 (A的独有+B的独有)
SELECT * FROM t_emp e LEFT JOIN t_dept d ON e.deptId = d.id WHERE d.id IS NULL
UNION ALL
SELECT * FROM t_emp e RIGHT JOIN t_dept d ON e.deptId = d.id WHERE e.deptId IS NULL;
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

「已注销」

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值