Postgresql:两个查询表,与相同的列名和显示的结果并肩命令将他们列名,其发生在两个表

0

的问题

具有两个表(table1, table2)与相同的列名(, 父母),所需的产出,将结合所有列两个表格中。 从而可行的 表2 应当加入 表1 所以,行 table2 是匹配的那些 table1 柱。 父数应下令上升为中的条目 table1 以及在 table2. 行的数量的查询结果应该是平等的那些 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             |

这个链接 导致的结论,即任何加入命令可能没有什么必要在这里...但联盟不仅追加行...所以对我来说这是绝对清楚,如何希望的结果可以实现的。O
任何帮助是高度赞赏。 在此先感谢!

join postgresql
2021-11-23 22:52:10
1

最好的答案

1

主要误会在这个问题出现了从事实上,你所提到的加入,这是一个非常精确的数学定义的概念基于笛卡尔的产品,并可应用于任何两套。 这样电流输出为明确。 但作为你写在标题,你想把两个表格的 . 你利用这一事实,他们有相同数量的行(三).

这种选择返回的输出你想要的。
我做了 人工加入列, row_number() OVER (order by generation, parent) as rnum和移动的第二个表中使用的另外的三个。 我希望这可以帮助你:

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
2021-11-25 21:38:39

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................