1

假设我们每个表有十行,AB A 有一个

ColA 
  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10

B带列的表格

ColB 
  11 
  12 
  13 
  14 
  15 
  16 
  17 
  18 
  19 
  20

需要输出:

SingleColumn
   1 
  11 
   2 
  12 
   3 
  13 
   4 
  14 
   5 
  15 
   6 
  16 
   7 
  17 
   8 
  18 
   9 
  19 
  10 
  20

PS:这两个表之间没有关系。两列都是独立的。也是 1, 2...19, 20 ,它们是 row id,如果考虑数据,则只有无序形式。

4

3 回答 3

2

更新在 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演示

于 2013-08-29T07:12:13.300 回答
1
SELECT col FROM (
  select colA as col
        ,row_number() over (order by colA) as order1
        ,1 as order2
  from tableA
  union all
  select colB
        ,row_number() over (order by colB)
        ,2
  from tableB
) order by order1, order2
于 2013-08-29T07:37:39.780 回答
0
select colA from tableA 
union 
select colB from tableB;
于 2013-08-29T07:06:15.460 回答