How can I join mysql?

I have two tables:
T1
1 and
2, b

T2
2, yyyy
3, hhh

I want the connection between them to give me all the fields:

1, a, NULL, NULL
2, b, 2, yyy
null, null, 3, hhh

+3
source share
2 answers

MySQL does not have FULL OUTER JOIN , but you can emulate it, for example:

SELECT * FROM T1 LEFT OUTER JOIN T2 ON T1.id = T2.id
UNION ALL
SELECT * FROM T1 RIGHT OUTER JOIN T2 ON T1.id = T2.id 
    WHERE T1.id IS NULL;

Generally:

FULL OUTER JOIN = LEFT OUTER JOIN ∪ (RIGHT OUTER JOIN ∖ INNER JOIN)

You need to shorten the inner join of one (here from the right join, but IMHO it doesn't matter which one you choose), because both return the inner joins of the same . Here you have:

T1 ::

enter image description here

2::

enter image description here

LEFT OUTER JOIN::

enter image description here

::

enter image description here

INNER JOIN::

enter image description here

::

enter image description here

+4

() , , . :

select  *
from    (
        select  col1
        from    t1
        union
        select  col1 
        from    t2
        ) ids
left join
        t1
on      ids.col1 = t1.col1
left join
        t2
on      ids.col1 = t2.col1
0

All Articles