postgresql:查询具有相同列名的两个表并并排显示结果,按列名排序,这些列名出现在两个表中

postgresql: query two tables with same column names and show the result side by side ordered their column names, which occur in both tables

有两个表(table1table2)具有相同的列名(generation , parent), 所需的输出将是两个表的所有列的组合。因此 table2 的行应该加入 table1 以便 table2 的行与 table1generation 列上。对于 table1table2 中的条目,父编号应按升序排列。查询结果的行数应该和table1.

相等

给定下表
表1:

| generation | parent |
|:----------:|:------:|
| 0          | 1      |
| 0          | 2      |
| 0          | 3      |
| 1          | 3      |
| 1          | 2      |
| 1          | 1      |
| 2          | 2      |
| 2          | 1      |
| 2          | 3      |

表2:

| generation | parent |
|:----------:|:------:|
| 1          | 3      |
| 1          | 1      |
| 1          | 3      |
| 2          | 1      |
| 2          | 2      |
| 2          | 3      |

考虑使用以下查询来创建和填充如上所示的两个示例表:

create table table1(generation integer, parent integer);
insert into table1 (generation, parent) values(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(2,2),(2,1),(2,3);
create table table2(generation integer, parent integer);
insert into table2 (generation, parent) values(1,3),(1,1),(1,3),(2,1),(2,2),(2,3);

想象的查询应该导致以下期望的结果

| table1_generation | table1_parent | table2_generation | table2_parent |
|:-----------------:|:-------------:|:-----------------:|:-------------:|
| 0                 | 1             |                   |               |
| 0                 | 2             |                   |               |
| 0                 | 3             |                   |               |
| 1                 | 1             | 1                 | 1             |
| 1                 | 2             | 1                 | 3             |
| 1                 | 3             | 1                 | 3             |
| 2                 | 1             | 2                 | 1             |
| 2                 | 2             | 2                 | 2             |
| 2                 | 3             | 2                 | 3             |

当前查询如下:

with 
  p as (
    select 
      generation,
      parent 
    from 
      table1
    order by
      generation,
      parent
  ), o as(
    select
      generation,
      parent 
    from 
      table2 
    order by
      generation,
      parent
  )
select
  p.generation as table1_generation,
  p.parent as table1_parent,
  o.generation as table2_generation,
  o.parent as table2_parent
from
  p
left join o on 
  o.generation=p.generation;

这导致以下结果:

| table1_generation | table1_parent | table2_generation | table2_parent |
|:-----------------:|:-------------:|:-----------------:|:-------------:|
| 0                 | 1             |                   |               |
| 0                 | 2             |                   |               |
| 0                 | 3             |                   |               |
| 1                 | 1             | 1                 | 1             |
| 1                 | 1             | 1                 | 3             |
| 1                 | 1             | 1                 | 3             |
| 1                 | 2             | 1                 | 1             |
| 1                 | 2             | 1                 | 3             |
| 1                 | 2             | 1                 | 3             |
| 1                 | 3             | 1                 | 1             |
| 1                 | 3             | 1                 | 3             |
| 1                 | 3             | 1                 | 3             |
| 2                 | 1             | 2                 | 1             |
| 2                 | 1             | 2                 | 2             |
| 2                 | 1             | 2                 | 3             |
| 2                 | 2             | 2                 | 1             |
| 2                 | 2             | 2                 | 2             |
| 2                 | 2             | 2                 | 3             |
| 2                 | 3             | 2                 | 1             |
| 2                 | 3             | 2                 | 2             |
| 2                 | 3             | 2                 | 3             |

This link 得出的结论是,任何连接命令都可能不是这里所必需的...但是 union 只会追加行...所以对我来说完全不清楚,期望的结果如何实现o.O
非常感谢任何帮助。提前致谢!

这个问题的主要误解是你提到了join,这是一个基于笛卡尔积的非常精确的数学定义概念,可以应用于任何两个集合。所以目前的输出是明确的。 但是正如你在标题中写的,你想放两个tables side by side。您利用了它们具有相同行数(三元组)这一事实。

这selectreturns你想要的输出。
我制作了 artificial join columnsrow_number() OVER (order by generation, parent) as rnum,并使用三个的加法移动了第二个 table。希望对您有所帮助:

with 
  p as (
    select 
      row_number() OVER (order by generation, parent) as rnum,
      generation,
      parent 
    from 
      table1
    order by
      generation,
      parent
  ), o as(
    select
      row_number() OVER (order by generation, parent) as rnum,
      generation,
      parent 
    from 
      table2 
    order by
      generation,
      parent
  )
select
  p.generation as table1_generation,
  p.parent as table1_parent,
  o.generation as table2_generation,
  o.parent as table2_parent
from
  p
left join o on 
  o.rnum+3=p.rnum
order by 1,2,3,4;

输出:

table1_generation table1_parent table2_generation table2_parent
0 1 (null) (null)
0 2 (null) (null)
0 3 (null) (null)
1 1 1 1
1 2 1 3
1 3 1 3
2 1 2 1
2 2 2 2
2 3 2 3