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

folder

During a table join, when does MySQL use this function?

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

I know what the function does, but I want to know when it is used during the join operation. This just makes no sense to me! Can someone show me an example?

Will B.

That is in reference to redundant column elimination during natural join and join with using. Describing how the columns are excluded from display.

The order of operation is described above the section you referenced.

  • 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

Example

t1

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

t2

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

The join with using

SELECT * FROM t1 JOIN t2 USING (b);

Would result in, t1.b being coalesced (due to USING), followed by the columns unique to the first table, followed by those in the second table.

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

Whereas a natural join

SELECT * FROM t1 NATURAL JOIN t2;

Would result in, the t1 columns (or rather common columns from both tables) being coalesced, followed by the unique columns of the first table, followed by those in the second table.

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related