MySQL三表查询实现全连接查询mysql三表查询全连接

在MySQL中,联合查询共分为三种方式:内连接查询、左连接查询和右连接查询。而全连接查询,是内连接查询和左连接查询的并集。也就是说,全连接查询会返回左表和右表中所有的行,如果在另一张表中也存在匹配的行,则一并返回。二、实现三表全连接查询的流程 本次使用以下三张表:students表 | id | ...
MySQL三表查询实现全连接查询mysql三表查询全连接
MySQL三表查询,实现全连接查询!
MySQL是一种常用的关系型数据库管理系统,它能够快速、高效地存储和获取数据。在实际应用中,常常需要对多张数据表进行联合查询。本文将介绍如何使用MySQL实现三表的全连接查询。
一、什么是全连接查询?
在MySQL中,联合查询共分为三种方式:内连接查询、左连接查询和右连接查询。而全连接查询,是内连接查询和左连接查询的并集。也就是说,全连接查询会返回左表和右表中所有的行,如果在另一张表中也存在匹配的行,则一并返回。
二、实现三表全连接查询的流程
本次使用以下三张表:
students表
| id | name | gender |
|—-|———-|——–|
| 1 | Tom | Male |
| 2 | Jerry | Male |
| 3 | Angela | Female |
| 4 | Ginger | Female |
| 5 | Hatchet | Male |
scores表
| id | student_id | language | score |
|—-|————|———-|——-|
| 1 | 1 | Chinese | 85 |
| 2 | 1 | English | 90 |
| 3 | 2 | Chinese | 70 |
| 4 | 2 | English | 80 |
| 5 | 3 | Chinese | 92 |
| 6 | 3 | English | 96 |
| 7 | 4 | Chinese | 78 |
| 8 | 5 | Chinese | 60 |
subjects表
| id | subject |
|—-|———|
| 1 | Chinese |
| 2 | English |
| 3 | Math |
我们的任务是,在三张表中联合查询学生的姓名、科目和成绩。
1. 创建三张表
CREATE TABLE students(
id INT(11) PRIMARY KEY,
name VARCHAR(20) NOT NULL,
gender VARCHAR(10)
);
CREATE TABLE scores(
id INT(11) PRIMARY KEY,
student_id INT(11) NOT NULL,
language VARCHAR(20),
score INT(11)
);
CREATE TABLE subjects(
id INT(11) PRIMARY KEY,
subject VARCHAR(20) NOT NULL
);
2. 插入数据
INSERT INTO students(id,name,gender) VALUES(1,’Tom’,’Male’);
INSERT INTO students(id,name,gender) VALUES(2,’Jerry’,’Male’);
INSERT INTO students(id,name,gender) VALUES(3,’Angela’,’Female’);
INSERT INTO students(id,name,gender) VALUES(4,’Ginger’,’Female’);
INSERT INTO students(id,name,gender) VALUES(5,’Hatchet’,’Male’);
INSERT INTO scores(id,student_id,language,score) VALUES(1,1,’Chinese’,85);
INSERT INTO scores(id,student_id,language,score) VALUES(2,1,’English’,90);
INSERT INTO scores(id,student_id,language,score) VALUES(3,2,’Chinese’,70);
INSERT INTO scores(id,student_id,language,score) VALUES(4,2,’English’,80);
INSERT INTO scores(id,student_id,language,score) VALUES(5,3,’Chinese’,92);
INSERT INTO scores(id,student_id,language,score) VALUES(6,3,’English’,96);
INSERT INTO scores(id,student_id,language,score) VALUES(7,4,’Chinese’,78);
INSERT INTO scores(id,student_id,language,score) VALUES(8,5,’Chinese’,60);
INSERT INTO subjects(id,subject) VALUES(1,’Chinese’);
INSERT INTO subjects(id,subject) VALUES(2,’English’);
INSERT INTO subjects(id,subject) VALUES(3,’Math’);
3. 三表联合查询
SELECT
students.name AS student_name,
subjects.subject AS subject_name,
scores.score AS score
FROM
students
FULL OUTER JOIN scores ON students.id = scores.student_id
FULL OUTER JOIN subjects ON scores.language = subjects.subject
ORDER BY
students.name,
subjects.subject;
结果如下:
| student_name | subject_name | score |
|————–|————-|——-|
| Angela | Chinese | 92 |
| Angela | English | 96 |
| Ginger | Chinese | 78 |
| Hatchet | Chinese | 60 |
| Jerry | Chinese | 70 |
| Jerry | English | 80 |
| Tom | Chinese | 85 |
| Tom | English | 90 |
| NULL | Math | NULL |
本次查询使用了FULL OUTER JOIN方法。该方法会返回两张表的所有行,如果某一方没有匹配的数据,则取NULL。当然,由于MySQL不支持FULL OUTER JOIN,我们可以使用UNION ALL连接笛卡尔积的左右联接并使用SELECT DISTINCT去除重复。
SELECT
students.name AS student_name,
subjects.subject AS subject_name,
scores.score AS score
FROM
students
LEFT JOIN scores ON students.id = scores.student_id
LEFT JOIN subjects ON scores.language = subjects.subject
UNION ALL
SELECT
students.name AS student_name,
subjects.subject AS subject_name,
scores.score AS score
FROM
students
RIGHT JOIN scores ON students.id = scores.student_id
RIGHT JOIN subjects ON scores.language = subjects.subject
WHERE
students.id IS NULL
ORDER BY
student_name,
subject_name;
结果和上述FULL OUTER JOIN的结果相同。
三、总结
本文介绍了MySQL中全连接查询的方法,以及如何使用三张表联合查询学生的姓名、科目和成绩。在实际应用中,三表及以上的联合查询比较常见,开发者们应掌握该技能,提高开发效率。2024-08-13
mengvlog 阅读 128 次 更新于 2025-09-08 01:21:34 我来答关注问题0
  • 在MySQL中,联合查询共分为三种方式:内连接查询、左连接查询和右连接查询。而全连接查询,是内连接查询和左连接查询的并集。也就是说,全连接查询会返回左表和右表中所有的行,如果在另一张表中也存在匹配的行,则一并返回。二、实现三表全连接查询的流程 本次使用以下三张表:students表 | id | ...

  • Tom | tom@gml.com | 1357924680 步骤4:完整的三表查询 如果需要显示来自每个表的所有记录,包括这些记录在其他表中不存在的情况,可以使用完整的外连接来执行三表查询。下面是使用完整的外连接实现全连接的查询代码。SELECT table1.user_name, table2.user_eml, table3.user_phone FROM table1...

  • 其中,FULL OUTER JOIN是MySQL中进行全连接的关键词,column_name是要查询的字段名,table1和table2是要进行连接的两个表。我们可以通过以下的代码实现三个表的联接:SELECT FROM student FULL OUTER JOIN score ON student.id = score.student_id FULL OUTER JOIN course ON score.course_id = course...

  • 在开发中,我们通常使用PHP语言作为MySQL数据库的客户端,通过PHP脚本来实现MySQL三表全连接的查询操作。下面,我们以PHP mysqli扩展为例,给出相关的代码实现。//连接MySQL数据库 mysqli = new mysqli(“localhost”, “username”, “password”, “database...

  • 1. 创建表 在进行全连接查询之前,我们需要先创建一个测试用的数据库。假设我们的数据库名字为test,并在该数据库中创建两张表:table1和table2。创建表的SQL语句如下:CREATE TABLE table1 (id INT PRIMARY KEY,name VARCHAR(50),age INT,gender VARCHAR(10));CREATE TABLE table2 (id INT ...

檬味博客在线解答立即免费咨询

mySQL相关话题

Copyright © 2023 WWW.MENGVLOG.COM - 檬味博客
返回顶部