I have two query results from different tables like below.
ID F1 F2 F3 ID F3 F4 F5
S1 d11 d12 d13 S2 d23 d24 d25 S2 d21 d22 d23 S3 d33 d34开发者_Go百科 d35 S3 d31 d32 d33 S4 d43 d44 d45
ID and F# are field names, the others are values. I want to get a new result in a similar form to the following from the results above.
ID F1 F2 F3 F4 F5
S1 d11 d12 d13 S2 d21 d22 d23 S2 d23 d24 d25 S3 d31 d32 d33 S3 d33 d34 d35 S4 d43 d44 d45
Is this possible in SQL? I'm programming in VBA with ADO. I've never done such a task before.
Note that the fields of the first two tables are dynamic. I can't predict how many there are or what they are.
select ID, F1, F2, F3, NULL as F4, NULL as F5
from Table1
union all
select ID, NULL as F1, NULL as F2, F3, F4, F5
from Table2
Maybe you could do a full outer join on two sub-queries that generate a fake join key that is never equal. This way you could use a select * to get any columns that exist in the two tables.
select * from
(select 1 as MY_ID, Table1.* from Table1) AS A
FULL OUTER JOIN
(select 2 as MY_ID, Table2.* from Table2) AS B
ON A.MY_ID = B.MY_ID
精彩评论