MySQL 是否在表间连接期间自动使用合并函数?

Does MySQL automatically use the coalesce function during a join between tables?

在 table 加入期间,MySQL 何时使用此功能?

The single result column that replaces two common columns is defined using the coalesce operation. That is, for two t1.a and t2.a the resulting single join column a is defined as a = COALESCE(t1.a, t2.a), where:

COALESCE(x, y) = (CASE WHEN x IS NOT NULL THEN x ELSE y END)

https://dev.mysql.com/doc/refman/8.0/en/join.html

我知道这个函数是干什么的,但是我想知道在join操作的时候是什么时候用到的。这对我来说毫无意义!有人可以给我举个例子吗?

参考自然连接和使用连接时的冗余列消除。描述列如何从显示中排除。

操作顺序在您引用的部分上方进行了描述。

  • First, coalesced common columns of the two joined tables, in the order in which they occur in the first table

  • Second, columns unique to the first table, in order in which they occur in that table

  • Third, columns unique to the second table, in order in which they occur in that table

例子

t1

| a | b | c |
| 1 | 1 | 1 |

t2

| a | b | d |
| 1 | 1 | 1 |

与using的连接

SELECT * FROM t1 JOIN t2 USING (b);

将导致 t1.b 合并(由于 USING),然后是第一个 table 独有的列,然后是第二个 table 中的列].

| b | a | c | a | d |
| 1 | 1 | 1 | 1 | 1 |

而自然连接

SELECT * FROM t1 NATURAL JOIN t2;

将导致合并 t1 列(或者更确切地说,来自两个 table 的公共列),然后是第一个 table 的唯一列,然后是那些在第二个 table.

| a | b | c | d |
| 1 | 1 | 1 | 1 |