想要合并两个表的列数据到另一张表中,但不想产生笛卡尔积
表A
列
a1
b1
表B
列
id1
id2
表C(也就是我要的结果)
列1 列2
a1 id1
a2 id2
要怎样写SQL来实现这样的结果?
------解决方案--------------------select
m.列 as 列1,
n.列 as 列2
from
(select 列,(select count(*) from A where 列 <=t.列) as ID from A t) m
full outer join
(select 列,(select count(*) from B where 列 <=t.列) as ID from B t) n
on
m.ID=n.ID
------解决方案----------------------如果都是自增列,且數據有可能不連續
Select
T1.列 As 列1, T2.列 As 列2
From
(Select OrderID = (Select Count(列) From A Where 列 <= T.列), * From A T) T1
Full Join
(Select OrderID = (Select Count(列) From B Where 列 <= T.列), * From B T) T2
On T1.OrderID = T2.OrderID