我需要在一个大表中链接2个表。 我的问题是我需要在一列(事物)中链接2个不同的列(例如:书籍,玩具)。 其他专栏:
实施例: 表1:
books cover price
----- ----- ------
book1 soft 19
book2 soft 23
book3 hard 39
表2:
toys name price
---- ---- -----
astro Buzz 29
mouse Jerr 35
结果:
things name cover price
------ ---- ----- -----
book1 null soft 19
book2 null soft 23
book3 null hard 39
astro Buzz null 29
mouse Jerr null 35
答案 0 :(得分:3)
您可以尝试使用UNION ALL
更多信息here
这样的事情:
SELECT books "things", NULL "name", cover, price
FROM table1
UNION ALL
SELECT toys "things", name , NULL "cover", price
FROM table2
答案 1 :(得分:0)
简单的UNION怎么样:
select books as things, null as name, cover, price
from table1
union
select toys as things, name, null as cover, price from table2
答案 2 :(得分:0)
你可以使用你的exmaple
这样使用Cross Applyselect * from table1 cross apply table2
或者您可以使用UNION ALL
SELECT books as 'THINGS', NULL as 'NAME', COVER, PRICE
FROM table1
UNION ALL
SELECT toys 'THINGS', NAME, NULL 'COVER', PRICE
FROM table2