I have two tables, first table contains 244 columns with 4945 records, where as in second table 11 columns with 3737 records, but 4 columns are common in both id, name, tocken, tockenold. How to combine this two tables
I tried with UNION but columns must be equal in both . Tried with full join getting the exact column and record count, but second table records are empty.
Can anyone please help me with this .
CodePudding user response:
try this:
select *
from (
select id, name, tocken, tockenold
from table_1
union
select id, name, tocken, tockenold
from table_2
) m
join table_1 t1
on m.id = t1.id and m.name = t1.name and m.tocken = t1.tocken and m.tockenold = t1.tockenold
join table_2 t2
on m.id = t2.id and m.name = t2.name and m.tocken = t2.tocken and m.tockenold = t2.tockenold
CodePudding user response:
Based on what you said:
I need to get 4945 3737 = 8682 records and 251 columns
Sounds like you just need to do manual union between the two tables:
SELECT
t1_c1, t1_c2, ..., t1_c240,
common_c1, common_c2, common_c3, common_c4,
null, null, null, null, null, null, null
FROM table1
UNION ALL
SELECT
null, null, .... null, -- 240 times
common_c1, common_c2, common_c3, common_c4,
t2_c1, t2_c2, ..., t2_c7
FROM table2
