如何在 MySQL 中执行 FULL OUTER JOIN?
我想要执行完全外部联接 MySQL。这可能吗? MySQL 支持全外连接吗?
I want to do a full outer join in MySQL. Is this possible? Is a full outer join supported by MySQL?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
MySQL 中没有完全连接,但您可以确定模拟它们。
对于从此堆栈溢出问题示例 a> 您有:
对于两个表 t1、t2:
上面的查询适用于完全外连接操作不会产生任何重复行的特殊情况。上面的查询依赖于 UNION 集合运算符来删除查询模式引入的重复行。我们可以通过对第二个查询使用反连接模式来避免引入重复行,然后使用 UNION ALL 集合运算符来组合两个集合。在更一般的情况下,完整的外连接会返回重复的行,我们可以这样做:
You don't have full joins in MySQL, but you can sure emulate them.
For a code sample transcribed from this Stack Overflow question you have:
With two tables t1, t2:
The query above works for special cases where a full outer join operation would not produce any duplicate rows. The query above depends on the
UNION
set operator to remove duplicate rows introduced by the query pattern. We can avoid introducing duplicate rows by using an anti-join pattern for the second query, and then use a UNION ALL set operator to combine the two sets. In the more general case, where a full outer join would return duplicate rows, we can do this:Pablo Santa Cruz 给出的答案是正确的;但是,如果有人偶然发现此页面并需要更多说明,这里有详细的细分。
示例表
假设我们有以下表:
内连接
内连接,如下所示:
只会获取两个表中都出现的记录,如下所示:
内连接没有方向(如左或右),因为它们是显式的双向 - 我们需要双方都匹配。
外连接
另一方面,外连接用于查找在另一个表中可能不匹配的记录。因此,您必须指定允许连接的哪一侧有缺失记录。
LEFT JOIN
和RIGHT JOIN
是LEFT OUTER JOIN
和RIGHT OUTER JOIN
的简写;我将在下面使用它们的全名来强化外连接与内连接的概念。左外连接
左外连接,如下所示:
...将从左表中获取所有记录,无论它们在右表中是否有匹配项,如下所示:
右外连接
右外连接,如this:
...将为我们提供右表中的所有记录,无论它们在左表中是否有匹配项,如下所示:
完全外连接
完全外连接将为我们提供两个表中的所有记录,无论是他们在另一个表中没有匹配项,在没有匹配项的两侧都有 NULL。结果如下所示:
然而,正如 Pablo Santa Cruz 指出的那样,MySQL 不支持这一点。我们可以通过执行左连接和右连接的 UNION 来模拟它,如下所示:
您可以将
UNION
视为“运行这两个查询,然后将结果堆叠在每个查询的顶部”其他”;一些行将来自第一个查询,一些来自第二个查询。应该注意的是,MySQL 中的 UNION 将消除精确的重复项:Tim 会出现在此处的两个查询中,但 UNION 的结果仅列出他一次。我的数据库专家同事认为不应依赖这种行为。因此,为了更明确地说明这一点,我们可以向第二个查询添加一个
WHERE
子句:另一方面,如果您出于某种原因想要查看重复项,您可以使用UNION ALL。
The answer that Pablo Santa Cruz gave is correct; however, in case anybody stumbled on this page and wants more clarification, here is a detailed breakdown.
Example Tables
Suppose we have the following tables:
Inner Joins
An inner join, like this:
Would get us only records that appear in both tables, like this:
Inner joins don't have a direction (like left or right) because they are explicitly bidirectional - we require a match on both sides.
Outer Joins
Outer joins, on the other hand, are for finding records that may not have a match in the other table. As such, you have to specify which side of the join is allowed to have a missing record.
LEFT JOIN
andRIGHT JOIN
are shorthand forLEFT OUTER JOIN
andRIGHT OUTER JOIN
; I will use their full names below to reinforce the concept of outer joins vs inner joins.Left Outer Join
A left outer join, like this:
...would get us all the records from the left table regardless of whether or not they have a match in the right table, like this:
Right Outer Join
A right outer join, like this:
...would get us all the records from the right table regardless of whether or not they have a match in the left table, like this:
Full Outer Join
A full outer join would give us all records from both tables, whether or not they have a match in the other table, with NULLs on both sides where there is no match. The result would look like this:
However, as Pablo Santa Cruz pointed out, MySQL doesn't support this. We can emulate it by doing a UNION of a left join and a right join, like this:
You can think of a
UNION
as meaning "run both of these queries, then stack the results on top of each other"; some of the rows will come from the first query and some from the second.It should be noted that a
UNION
in MySQL will eliminate exact duplicates: Tim would appear in both of the queries here, but the result of theUNION
only lists him once. My database guru colleague feels that this behavior should not be relied upon. So to be more explicit about it, we could add aWHERE
clause to the second query:On the other hand, if you wanted to see duplicates for some reason, you could use
UNION ALL
.使用联合查询将删除重复项,这与完全外连接从不删除任何重复项的行为不同:
这是完全外连接的预期结果外连接:
这是使用左和右连接与联合的结果:
SQL Fiddle
我建议的查询是:
上述查询的结果与预期结果相同:
SQL Fiddle
注意:这可能是最好的解决方案,无论是效率还是生成与
FULL OUTER JOIN
相同的结果。 这篇博文 也很好地解释了这一点 - 引用方法 2:“这会正确处理重复的行,并且不包含任何不应包含的内容。有必要使用UNION ALL
而不是普通的 < code>UNION,这将消除我想要保留的重复项,这对于大型结果集可能会更加有效,因为不需要排序和删除重复项。”我决定添加另一个解决方案。来自完全外连接可视化和数学。它并不比上面的更好,但更具可读性:
SQL Fiddle
Using a union query will remove duplicates, and this is different than the behavior of full outer join that never removes any duplicates:
This is the expected result of a full outer join:
This is the result of using left and right join with union:
SQL Fiddle
My suggested query is:
The result of the above query that is as the same as the expected result:
SQL Fiddle
Note: This may be the best solution, both for efficiency and for generating the same results as a
FULL OUTER JOIN
. This blog post also explains it well - to quote from Method 2: "This handles duplicate rows correctly and doesn’t include anything it shouldn’t. It’s necessary to useUNION ALL
instead of plainUNION
, which would eliminate the duplicates I want to keep. This may be significantly more efficient on large result sets, since there’s no need to sort and remove duplicates."I decided to add another solution that comes from full outer join visualization and math. It is not better than the above, but it is more readable:
SQL Fiddle
前面的答案实际上都不正确,因为当存在重复值时它们不遵循语义。
对于诸如(来自 此重复)的查询:
正确的等效项是:
如果您需要它与
NULL
值一起使用(这可能也是必要的),然后使用NULL
安全比较运算符<=>
而不是=
。None of the previous answers are actually correct, because they do not follow the semantics when there are duplicated values.
For a query such as (from this duplicate):
The correct equivalent is:
If you need this to work with
NULL
values (which may also be necessary), then use theNULL
-safe comparison operator,<=>
rather than=
.MySQL 没有 FULL-OUTER-JOIN 语法。您必须通过执行 LEFT JOIN 和 RIGHT JOIN 来模拟它,如下所示:
但 MySQL 也没有 RIGHT JOIN 语法。根据MySQL的外连接简化,正确的通过在查询中的
FROM
和ON
子句中切换 t1 和 t2,join 将转换为等效的左联接。因此,MySQL 查询优化器将原始查询转换为以下内容 -现在,按原样编写原始查询没有什么坏处,但是如果您有像 WHERE 子句这样的谓词,即 before-join 谓词或 AND 谓词
ON
子句,这是一个 during-join谓词,那么你可能想看看魔鬼;这是详细信息。MySQL 查询优化器会例行检查谓词是否拒绝 null。
现在,如果您已完成 RIGHT JOIN,但在 t1 的列上使用 WHERE 谓词,那么您可能面临null-rejected的风险设想。
例如,
查询优化器将查询转换为以下内容:
因此表的顺序已更改,但谓词仍应用于 t1,但 t1 现在位于“ON”子句中。如果 t1.col1 定义为
NOT NULL
列,那么该查询将被拒绝 null。
任何拒绝 null 的外连接(左、右、全)都会被 MySQL 转换为内连接。
因此,您期望的结果可能与 MySQL 返回的结果完全不同。您可能认为这是 MySQL 的 RIGHT JOIN 的错误,但这是不对的。这就是 MySQL 查询优化器的工作原理。因此,负责的开发人员在构建查询时必须注意这些细微差别。
MySQL does not have FULL-OUTER-JOIN syntax. You have to emulate it by doing both LEFT JOIN and RIGHT JOIN as follows:
But MySQL also does not have a RIGHT JOIN syntax. According to MySQL's outer join simplification, the right join is converted to the equivalent left join by switching the t1 and t2 in the
FROM
andON
clause in the query. Thus, the MySQL query optimizer translates the original query into the following -Now, there is no harm in writing the original query as is, but say if you have predicates like the WHERE clause, which is a before-join predicate or an AND predicate on the
ON
clause, which is a during-join predicate, then you might want to take a look at the devil; which is in details.The MySQL query optimizer routinely checks the predicates if they are null-rejected.
Now, if you have done the RIGHT JOIN, but with WHERE predicate on the column from t1, then you might be at a risk of running into a null-rejected scenario.
For example, the query
gets translated to the following by the query optimizer:
So the order of tables has changed, but the predicate is still applied to t1, but t1 is now in the 'ON' clause. If t1.col1 is defined as
NOT NULL
column, then this query will be null-rejected.
Any outer-join (left, right, full) that is null-rejected is converted to an inner-join by MySQL.
Thus the results you might be expecting might be completely different from what the MySQL is returning. You might think its a bug with MySQL's RIGHT JOIN, but that’s not right. Its just how the MySQL query optimizer works. So the developer in charge has to pay attention to these nuances when he/she is constructing the query.
我修改了 shA.t 的查询为了更清楚:
I modified shA.t's query for more clarity:
在 SQLite 中你应该这样做:
In SQLite you should do this:
您可以将完整的外部联接转换
为:
或者,如果您在
firsttable
中至少有一个列,例如foo
,且该列不为 NULL,您可以执行以下操作:You can just convert a full outer join, e.g.
into:
Or if you have at least one column, say
foo
, infirsttable
that is NOT NULL, you can do:您可以执行以下操作:
You can do the following:
表
L
和R
之间的完全外连接包括:L x R
的行,其中谓词匹配,如l, r
L
中剩余的行(如果有),如l, r
R
中的剩余行(如果有),如l, r< col1, col2, ...>
要模拟此行为:
左连接
组合设置 #1 和 #2 来实现来实现不存在
union all
合并两个集合查询
示例数据和结果
DB<>小提琴
Full outer join between table
L
andR
consists of:L x R
where on predicate matches, asl<col1, col2, ...>, r<col1, col2, ...>
L
if any, asl<col1, col2, ...>, r<null, null, ...>
R
if any, asl<null, null, ...>, r<col1, col2, ...>
To emulate this behavior:
left join
not exists
union all
query
sample data and result
DB<>Fiddle
您可以使用 union all 而不是完全联接。我会做这样的事情:
在这里,“名称”列相当于要加入的列。
这样做的好处是不需要加入两次,只需要分组一次。
You can use union all instead of full join. I'd do something like this:
Here, the "name" column is equivalent to the column to join.
The advantage of this is you don't need to join twice, you only need to group by once.
使用:
可以按如下方式重新创建:
使用 UNION 或 UNION ALL 答案不涵盖基表具有重复条目的边缘情况。
说明:
存在 UNION 或 UNION ALL 无法覆盖的边缘情况。我们无法在 MySQL 上测试这一点,因为它不支持完全外连接,但我们可以在支持它的数据库上说明这一点:
这给了我们这个答案:
UNION 解决方案:
给出了错误的答案:
UNION ALL 解决方案:
是也不正确。
而此查询:
给出以下内容:
顺序不同,但在其他方面与正确答案匹配。
Use:
It can be recreated as follows:
Using a UNION or UNION ALL answer does not cover the edge case where the base tables have duplicated entries.
Explanation:
There is an edge case that a UNION or UNION ALL cannot cover. We cannot test this on MySQL as it doesn't support full outer joins, but we can illustrate this on a database that does support it:
This gives us this answer:
The UNION solution:
Gives an incorrect answer:
The UNION ALL solution:
Is also incorrect.
Whereas this query:
Gives the following:
The order is different, but otherwise matches the correct answer.
也是可以的,但是你必须在 select 中提及相同的字段名称。
It is also possible, but you have to mention the same field names in select.