数据库 
首页 > 数据库 > 浏览文章

MySQL多表查询详解上

(编辑:jimmy 日期: 2024/11/16 浏览:3 次 )

时光在不经意间,总是过得出奇的快。小暑已过,进入中暑,太阳更加热烈的绽放着ta的光芒,...在外面被太阳照顾的人们啊,你们都是勤劳与可爱的人啊。在房子里已各种姿势看我这篇这章的你,既然点了进来,那就由我继续带你回顾MySql的知识吧!

回顾练习资料girls库以及两张表的脚本:

链接: https://pan.baidu.com/s/1bgFrP7dBBwk3Ao755pU4Qg 提取码: ihg7

引题:笛卡尔现象,先来观看一下两张表。

SELECT * FROM boys;

MySQL多表查询详解上

SELECT * FROM beauty;

MySQL多表查询详解上

SELECT NAME,boyname FROM boys,beauty;
最终结果:12*4=48行

MySQL多表查询详解上

#进阶6:连接查询
含义:又称多表查询,当查询的字段来自于多个表时,就会用到连接查询

笛卡儿积现象: 表1 有m行,表2有n行,结果=m*n行
产生原因:没有有效的连接条件
解决方法:添加有效的连接条件
 连接分类:
   按年代分类:
   SQL1992标准(192标准):仅支持内连接
   SQL1999标准(199标准) [推荐]:支持内连接+外联结(左外与右外)+交叉连接

  按功能分类:
   内连接:
     等值连接
     非等值连接
     自联结
   外连接:
     左外连接
     右外连接
     全外连接
    交叉连接:
      左外连接
      右外连接
      全外连接
   交叉连接:
SELECT NAME,boyname FROM boys,beauty
WHERE beauty.boyfriend_id = boys.id;

MySQL多表查询详解上

#一.SQL192标准
#1.等值连接
 ①多表等值连接的结果为多表的交集部分
 ②n表连接,至少需要n-1个连接条件
 ③多表的顺序没有要求
 ④一般需要为表起别名
 ⑤可以搭配前面介绍的所有子句使用,比如,排序,分组,筛选。
#多表查询,先匹配在筛选
#案例1.查询员工名和对应的部门名。
SELECT first_name AS 名,department_name AS 部门名
FROM employees,departments
WHERE employees.department_id = departments.department_id;

MySQL多表查询详解上

#案例2.查询员工名,工种号,工种名。对于两张表共有的字段需要加表名作限定不然会报错。
错误示例:
SELECT first_name AS 名,employees.job_id AS 工种号,job_title AS 工种名
FROM employees,jobs
WHERE employees.job_id = jobs.job_id;
#2.为表起别名
 ①提高语句的简洁度
 ②区分多个重命名的字段
注意:如果为表起了别名,则查询的字段就不能使用原来的表名去限定
SELECT first_name AS 名,e.job_id AS 工种号,job_title AS 工种名
FROM employees AS e,jobs AS j
WHERE e.job_id = j.job_id;

MySQL多表查询详解上

给表起了别名,再用表完整名子做限定会报错,不允许。根据执行顺序走先走FROM,
走完FROM后就用别名的,相当于生成了一个虚拟的视图,不再认原来的表名。

MySQL多表查询详解上

#3.两个表名的顺序是否可以调换,是可以调换的。
SELECT first_name AS 名,e.job_id AS 工种号,job_title AS 工种名
FROM jobs AS j,employees AS e
WHERE e.job_id = j.job_id;

#4.可以加筛选
#案例3.查询有奖金的员工名,部门名。
SELECT first_name AS 名,department_name AS 部门名,commission_pct AS 奖金
FROM employees AS e,departments AS d
WHERE e.department_id=d.department_id
AND commission_pct IS NOT NULL;#AND e.commission_pct IS NOT NULL;

MySQL多表查询详解上

#案例4.查询城市名中第二个字符为o对应的城市名与部门名。
SELECT city AS 城市,department_name AS 部门名
FROM locations AS l,departments AS d
WHERE l.location_id = d.location_id
AND city LIKE '_o%';

MySQL多表查询详解上

#5.可以加分组
#案例1.查询每个城市的部门个数。
SELECT city AS 城市,COUNT(department_id) AS 个数
FROM locations AS l,departments AS d
WHERE l.location_id = d.location_id
GROUP BY l.city;

MySQL多表查询详解上

#案例2.查询有奖金的每个部门的部门名和部门的领导编号和该部门的最低工资。
#查询的时候不确定把两个列都加上。
SELECT commission_pct AS 奖金,department_name AS 部门名,
d.manager_id AS 领导编号,MIN(salary) AS 最低工资
FROM employees AS e,departments AS d
WHERE e.department_id = d.department_id
AND commission_pct IS NOT NULL
GROUP BY department_name,d.manager_id;

MySQL多表查询详解上

#6.可以加排序
#案例1:查询每个工种的工种名和员工的个数,并且按员工个数降序。
SELECT j.job_title AS 工种名,COUNT(employee_id) AS 个数
FROM employees AS e,jobs AS j
WHERE e.job_id = j.job_id
GROUP BY job_title
ORDER BY 个数 DESC;

MySQL多表查询详解上

#7.三表连接
#案例1.查询员工名,部门名与所在的城市
SELECT first_name AS 名,d.manager_id AS 部门名,city AS 城市
FROM employees AS e,departments AS d,locations AS l
WHERE e.department_id = d.department_id
 AND d.location_id = l.location_id;

MySQL多表查询详解上

#案例2.查询员工名,部门名与所在的城市,城市以s开头。
SELECT first_name AS 名,d.manager_id AS 部门名,city AS 城市
FROM employees AS e,departments AS d,locations AS l
WHERE e.department_id = d.department_id
AND d.location_id = l.location_id
 AND city LIKE 's%';

MySQL多表查询详解上

#案例3.查询员工名,部门名与所在的城市,城市以s开头,按姓名降序排列。
SELECT first_name AS 名,d.manager_id AS 部门名,city AS 城市
FROM employees AS e,departments AS d,locations AS l
WHERE e.department_id = d.department_id
AND d.location_id = l.location_id
AND city LIKE 's%'
ORDER BY department_name DESC;

MySQL多表查询详解上

新增一张工资等级表。
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);

MySQL多表查询详解上

#2.非等值连接,(范围判断)
#案例1.查询员工的工资和工资级别。
SELECT salary AS 工资,grade_level AS 等级
FROM employees AS e,job_grades AS g
WHERE salary BETWEEN g.lowest_sal AND g.highest_sal;

MySQL多表查询详解上

#案例2.查询员工的工资和工资级别,展示出A级别的员工。
SELECT salary AS 工资,grade_level AS 等级
FROM employees AS e,job_grades AS g
WHERE salary BETWEEN g.lowest_sal AND g.highest_sal
AND g.grade_level = 'A';

MySQL多表查询详解上

#3.自联接[自己连接自己]

MySQL多表查询详解上

#案例1.查询员工名和上级的名称.
SELECT e.employee_id,e.last_name AS 员工,
m.employee_id,m.last_name AS 领导
FROM employees e,employees m
WHERE e.manager_id = m.employee_id;

MySQL多表查询详解上

经过了以上的示例,相信你对多表查询已经有了一个了解,赶快动动你的小手手来练习一下吧!o(^▽^)o

上一篇:浅谈mysql通配符进行模糊查询的实现方法
下一篇:mysql 8.0.21 安装配置方法图文教程
一句话新闻
微软与英特尔等合作伙伴联合定义“AI PC”:键盘需配有Copilot物理按键
几个月来,英特尔、微软、AMD和其它厂商都在共同推动“AI PC”的想法,朝着更多的AI功能迈进。在近日,英特尔在台北举行的开发者活动中,也宣布了关于AI PC加速计划、新的PC开发者计划和独立硬件供应商计划。
在此次发布会上,英特尔还发布了全新的全新的酷睿Ultra Meteor Lake NUC开发套件,以及联合微软等合作伙伴联合定义“AI PC”的定义标准。
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 站点导航 SiteMap