Chuy Garza
Chuy Garza

Reputation: 1

Two tables union

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!

Upvotes: 0

Views: 242

Answers (2)

Engr Syed Rowshan Ali
Engr Syed Rowshan Ali

Reputation: 784

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

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1269693

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

Upvotes: 1

Related Questions