我无法理解的简单问题。 我想结合两个没有共同数据的表格。
table1 table2 -> result
a b e f a b e f
c d g h c d g h
i j \n \n i j
答案 0 :(得分:0)
SQL表本质上是无序的,因此需要一个用于排序行的键。您可以使用变量创建一个。
您的表格具有不同的行数。这意味着full outer join
是可取的,但在MySQL中不可用。因此,您可以使用聚合和union all
:
select max(col1) as col1, max(col2) as col2, max(col3) as col3, max(col4) as col4
from ((select col1, col2, null as col3, null as col4, @rn1 := @rn1 + 1 as rn
from table1 cross join (select @rn1 := 0) vars
) union all
(select null, null, col3, col4, @rn2 := @rn2 + 1 as rn
from table2 cross join (select @rn2 := 0) vars
)
) tt
group by rn
答案 1 :(得分:0)
您可以使用RIGHT JOIN
执行此操作,并使用ROW_NUMBER
为每个表分配ID。请参阅下面的查询:
SELECT A.column1,A.column2,B.column1,B.column2 FROM
(SELECT
@row_number1:=@row_number1+1 AS RowNumber1,
column1,
column2
FROM Table1, (SELECT @row_number1:=0)AS x ORDER BY column1) AS A
RIGHT JOIN
(SELECT
@row_number2:=@row_number2+1 AS RowNumber2,
column1,
column2
FROM Table2, (SELECT @row_number2:=0)AS y ORDER BY column1) AS B
ON A.RowNumber1=B.RowNumber2