从 SQL 服务器中的多个 table 按顺序获取行

Get rows in sequence from multiple table in SQL Server

我想从 SQL 服务器中的 2 table 秒开始按顺序获取所有行。

输出应该是第 1 行从第 1 table,然后是第 1 行从第 2 table, 第 2 行从第 1 table,第 2 行从第 2 table...等

output should be 1st row from 1st table, then 1st row from 2nd table,
2nd row from 1st table, 2nd row from 2nd table....etc

可能是这样的:

select * from 
(
select rowID,Row,z from table1
union all
select rowID,Row,z from table2
) alltables
order by z

@eshirvana 建议的内容不会让您满意。相反,它将是 table1.row1, table2.row1, table2.row2, table1.row2

当列名和类型匹配时,您可以使用 UNION 连接两个表中的数据。我正在假设如何根据您想要的结果对数据进行排序。

SELECT RowID, Row, z
FROM table1 
UNION
SELECT *
FROM table2
ORDER BY z, RowID

这是工作代码:

https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=068c0fd2056cc48718345e85b74b7bba

您可以尝试以下方法:

SELECT * FROM
(
SELECT RowId,Row,Z,1 AS TableOrder From Table1
UNION ALL
SELECT RowId,Row,z,2 AS TableOrder From Table2
)
ORDER BY Z,TableOrder