sql >> Base de Datos >  >> RDS >> Mysql

¿Cómo hacer una combinación externa completa para combinar dos tablas en mysql?

Para hacer FULL OUTER JOIN puede hacer la LEFT OUTER JOIN y UNION con RIGHT OUTER JOIN (siempre que MySql todavía no es compatible con FULL OUTER JOIN ):

select * from A as a
    left outer join B as b on a.col = b.col
union
select * from A as a
    right outer join B as b on a.col = b.col

Tenga en cuenta que puede usar subconsultas para A y B - que debería funcionar con sus consultas. En su caso:

select * from (SELECT * FROM t1) as a
    left outer join (SELECT * FROM t2) as b on a._tid = b._tid
union
select * from (SELECT * FROM t1) as a
    right outer join (SELECT * FROM t2) as b on a._tid = b._tid

Con un resultado igual a (siempre que no haya cometido un error al copiar y pegar sus datos):

+------+-------+------------+----------+------+-------+------------+----------+
| _id  | _tid  | _dt        | _advance | _id  | _tid  | _dt        | _advartn |
+------+-------+------------+----------+------+-------+------------+----------+
|   17 | hjg   | 2012-04-18 |     2151 | NULL | NULL  | NULL       |     NULL |
|   22 | RKT01 | 2012-04-10 |     2098 | NULL | NULL  | NULL       |     NULL |
|   14 | RKT04 | 2012-04-18 |     1511 |    8 | RKT04 | 2012-04-20 |      150 |
|   16 | RKT09 | 2012-04-09 |      250 | NULL | NULL  | NULL       |     NULL |
|   15 | RKT10 | 2012-04-17 |     1313 | NULL | NULL  | NULL       |     NULL |
|    8 | RKT21 | 2012-04-03 |     1321 | NULL | NULL  | NULL       |     NULL |
|   19 | RKT31 | 2012-04-26 |     2512 | NULL | NULL  | NULL       |     NULL |
|   20 | RKT33 | 2012-04-10 |     2250 | NULL | NULL  | NULL       |     NULL |
|   25 | T01   | 2012-04-11 |     2500 | NULL | NULL  | NULL       |     NULL |
| NULL | NULL  | NULL       |     NULL |    9 | RKT02 | 2012-04-10 |     2500 |
+------+-------+------------+----------+------+-------+------------+----------+