当sql中的表之间没有关系时,如何合并两个表的行

时间:2022-09-05 15:38:24

Suppose We have ten rows in each table A and table B table A with single

假设我们在每个表A中有十行,而表B有一个表

ColA 
  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10

and table B with column

和表B列

ColB 
  11 
  12 
  13 
  14 
  15 
  16 
  17 
  18 
  19 
  20

Output required:

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

P.S : There is no relation between the two table. Both columns are independent. Also 1, 2...19, 20 , they are row ids and if considered the data only then in an unordered form.

P.S:两张桌子之间没有关系。两列都是独立的。同样是1,2 ... 19,20,它们是行ID,如果仅考虑数据,则以无序形式。

3 个解决方案

#1


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

#2


2  

UPDATED In SQL Server and Oracle you can do it like this

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

Output:

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

Here is SQLFiddle demo (SQL Server)
Here is SQLFiddle demo (Oracle)

这是SQLFiddle演示(SQL Server)这里是SQLFiddle演示(Oracle)

In MySql you can do

在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

Here is SQLFiddle demo

这是SQLFiddle演示

#3


0  

select colA from tableA 
union 
select colB from tableB;

#1


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

#2


2  

UPDATED In SQL Server and Oracle you can do it like this

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

Output:

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

Here is SQLFiddle demo (SQL Server)
Here is SQLFiddle demo (Oracle)

这是SQLFiddle演示(SQL Server)这里是SQLFiddle演示(Oracle)

In MySql you can do

在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

Here is SQLFiddle demo

这是SQLFiddle演示

#3


0  

select colA from tableA 
union 
select colB from tableB;