JIYIK CN >

Current Location:Home > Learning > DATABASE > MySQL >

Full Join in MySQL

Author:JIYIK Last Updated:2025/04/24 Views:

This article aims to explore how to perform a full join or full outer join in MySQL.

Full outer join is used to merge or combine the entire data from two separate tables. For example, suppose we have two tables named student_idand student_namewhich have a common column.

We can completely merge these two tables in MySQL by matching the values ​​of the common columns and UNION, LEFT JOINand . Although the process may seem complicated at first glance, let's understand it step by step.RIGHT JOIN

Before we begin, we will student_detailscreate a dummy dataset by creating a table with a few rows.

-- create the table student_details
CREATE TABLE student_details(
  stu_id int,
  stu_firstName varchar(255) DEFAULT NULL,
  stu_lastName varchar(255) DEFAULT NULL,
  primary key(stu_id)
);
-- insert rows to the table student_details
INSERT INTO student_details(stu_id,stu_firstName,stu_lastName) 
 VALUES(1,"Preet","Sanghavi"),
 (2,"Rich","John"),
 (3,"Veron","Brow"),
 (4,"Geo","Jos"),
 (5,"Hash","Shah"),
 (6,"Sachin","Parker"),
 (7,"David","Miller");

To see the entries in the data, we use the following code.

SELECT * FROM student_details;

Output:

stu_id	stu_firstName	stu_lastName
1	      Preet	        Sanghavi
2	      Rich	        John
3	      Veron	        Brow
4	      Geo	        Jos
5	      Hash	        Shah
6	      Sachin	    Parker
7	      David	        Miller

We need another student_markstable called that contains stu_idthe marks for each student corresponding to . We can create such a table using the following query.

-- create the table student_marks
CREATE TABLE student_marks(
  stu_id int,
  stu_marks int
);
-- insert rows to the table student_marks
INSERT INTO student_marks(stu_id,stu_marks) 
 VALUES(1,10),
 (2,20),
 (3,30),
 (4,7),
 (5,9),
 (6,35),
 (7,15);

We can visualize this table with the help of the following query.

SELECT * from student_marks;

Output:

stu_id  stu_marks
1		10
2		20
3		30
4		7
5		9
6		35
7		15

Let our goal be to perform a full outer join on the student_detailsand student_markstables, with stu_idas the common column in both tables.

FULL JOINStatements in MySQL

FULL OUTER JOINThe basic syntax of the technique is as follows.

SELECT * FROM table_1
LEFT JOIN table_2 ON table_1.id = table_2.id
UNION
SELECT * FROM table_1
RIGHT JOIN table_2 ON table_1.id = table_2.id

As shown in the above query, our goal is to join two tables named and based on the common column with the help of 左连接and .右连接idtable_1table_2

We can use the following query to solve the problem student_detailswith the and student_markstables.

-- Full Join using UNION
SELECT * FROM student_details
LEFT JOIN student_marks ON student_details.stu_id = student_marks.stu_id
UNION
SELECT * FROM student_details
RIGHT JOIN student_marks ON student_details.stu_id = student_marks.stu_id

As shown in the above code, we are stu_idmerging the two tables under consideration based on the column. The output of the above code is as follows.

stu_id	stu_firstName	stu_lastName  stu_id	stu_marks
1		Preet			Sanghavi		1		10
2		Rich			John			2		20
3		Veron			Brow			3		30
4		Geo				Jos				4		7
5		Hash			Shah			5		9
6		Sachin			Parker			6		35
7		David			Miller			7		15

It can be seen from the output block stu_marksthat after the full outer join , stu_idit is correctly assigned to each student according to .

So, with the help of the statement and the AND UNIONon two different tables , we can effectively create a full outer join in MySQL without any duplicate rows.左连接右连接

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Get the version in MySQL

Publish Date:2025/04/24 Views:169 Category:MySQL

In this article, we will learn how to get the current version of MySQL on Windows systems. It is necessary for businesses to maintain versions of different tools and software used to run their business in order to keep systems compatible. T

How to drop all tables in MySQL

Publish Date:2025/04/24 Views:196 Category:MySQL

This article demonstrates several ways for users to delete all tables in MySQL and lists sample scripts to implement this method. The reason you can't just drop all the tables in one line of code is that in a large and well-designed databas

Display tables and database structure in MySQL

Publish Date:2025/04/23 Views:97 Category:MySQL

Today, we will learn about queries in MySQL that can display the table and database structure. We will use the mysqldump utility, DESCRIBE the , SHOW TABLES and SHOW CREATE TABLE the statements. We are using MySQL version 8.0.28 while writi

Select first row from MySQL table

Publish Date:2025/04/23 Views:112 Category:MySQL

Today, we will explore three scenarios and their solutions where we want to select the first row from a MySQL table. In the first scenario, we will learn to get the first row from a MySQL table where there are multiple instances of a partic

Insert timestamp into MySQL table

Publish Date:2025/04/23 Views:78 Category:MySQL

Today, we will learn how to TIMESTAMP insert date and time into a type column of a MySQL table according to the table definition. Create a MySQL table First, we will create the tables that we will use in this tutorial. Sample code: CREATE T

The difference between two tables in MySQL

Publish Date:2025/04/23 Views:102 Category:MySQL

In this article, we will learn how to find the difference between two tables in MySQL. The difference between two tables in MySQL We often need to compare two tables to find records in one table that have no matching records in the other ta

MySQL sorts data alphabetically

Publish Date:2025/04/23 Views:130 Category:MySQL

In this article, we will learn about various ways to sort data alphabetically in MySQL. Sort MySQL data alphabetically When you use the SELECT command to query data from a table, the rows in the result set are in arbitrary order. To order t

Display the current database in MySQL

Publish Date:2025/04/23 Views:199 Category:MySQL

This article focuses on the various queries that can be used to display the current database in MySQL. We will learn by using the Windows command line and MySQL Workbench. Display the current database in MySQL We can use the following query

Check if a database exists in MySQL

Publish Date:2025/04/23 Views:179 Category:MySQL

In this article, we will introduce many ways to check if a database exists in MySQL. Check if the database exists in MySQL The system schema is the schema that MySQL uses. It includes tables that contain data needed by a running MySQL serve

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial