更新在 SQL Server 和 Oracle 中,您可以这样做
SELECT col
FROM
(
SELECT a.*
FROM
(
SELECT cola col, 1 source, ROW_NUMBER() OVER (ORDER BY cola) rnum
FROM tablea
) a
UNION ALL
SELECT b.*
FROM
(
SELECT colb col, 2 source, ROW_NUMBER() OVER (ORDER BY colb) rnum
FROM tableb
) b
) c
ORDER BY rnum, source
输出:
| 色彩 |
|-----|
| 1 |
| 11 |
| 2 |
| 12 |
| 3 |
| 13 |
| 4 |
| 14 |
| 5 |
| 15 |
| 6 |
| 16 |
| 7 |
| 17 |
| 8 |
| 18 |
| 9 |
| 19 |
| 10 |
| 20 |
这是SQLFiddle演示(SQL Server)
这是SQLFiddle演示(Oracle)
在MySql中你可以做
SELECT col
FROM
(
(
SELECT cola col, 1 source, @n := @n + 1 rnum
FROM tablea CROSS JOIN (SELECT @n := 0) i
ORDER BY cola
)
UNION ALL
(
SELECT colb col, 2 source, @m := @m + 1 rnum
FROM tableb CROSS JOIN (SELECT @m := 0) i
ORDER BY colb
)
) c
ORDER BY rnum, source
这是SQLFiddle演示