Home > Database > Mysql Tutorial > How Can I Perform a FULL OUTER JOIN in MySQL?

How Can I Perform a FULL OUTER JOIN in MySQL?

Mary-Kate Olsen
Release: 2025-01-09 20:46:44
Original
476 people have browsed it

How Can I Perform a FULL OUTER JOIN in MySQL?

MySQL Doesn't Support FULL OUTER JOIN: A Solution

Standard SQL's FULL OUTER JOIN isn't directly supported in MySQL. The provided SQL statement will result in a syntax error.

Alternative Approach: Combining LEFT and RIGHT JOINs

To achieve the equivalent of a FULL OUTER JOIN in MySQL, you can combine LEFT JOIN and RIGHT JOIN operations using UNION. This approach effectively merges results from both joins to include all rows from both tables, matching where possible.

Example using UNION:

<code class="language-sql">SELECT * FROM a
LEFT JOIN b ON a.id = b.id
UNION
SELECT * FROM a
RIGHT JOIN b ON a.id = b.id</code>
Copy after login

This query first performs a LEFT JOIN (all rows from the left table, a, and matching rows from the right table, b). Then, a RIGHT JOIN (all rows from the right table, b, and matching rows from the left table, a) is executed. Finally, UNION combines the results, eliminating duplicate rows. This effectively mirrors the functionality of a FULL OUTER JOIN.

The above is the detailed content of How Can I Perform a FULL OUTER JOIN in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template