0

I need to join two tables, example below:

table 1

K a b c
P x x x
P x x x
P x x x

table 2

K a b c 
P x x x
P x x x
P x x x

Final table

K a b c d e f
P x x x . . .
P x x x . . .
P x x x . . .
P . . . x x x
P . . . x x x
P . . . x x x

All the unions I've tried don't get my result properly.

Thanks!

clinomaniac
  • 2,200
  • 2
  • 17
  • 22

2 Answers2

1

The simplest way, in my opinion, is to use full join:

select t1.*, t2.*
from table1 t1 full join
     table2 t2
     on 1 = 0;   -- never true
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
1

You may try other answers also, This one particularly answer your question and is faster than join. Because there is no where condition

SELECT t1.p, t1.a, t1.b, t1.c, '0' d, '0' e, '0' f
FROM table1 t1 
UNION ALL
SELECT t2.p, '0' a, '0' b, '0' c, t2.a d, t2.b e, t2.c f 
FROM table2 t2