简单的问题,我不能把我的头。我只想将没有公共数据的两个表并排组合。
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发布于 2015-02-24 00:18:34
您可以使用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发布于 2015-02-24 00:16:45
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 rnhttps://stackoverflow.com/questions/28685992
复制相似问题