繁体   English   中英

如何连接两个表并创建新列?

[英]How to join two tables and create new column?

使用 PostgreSQL 10.4。

我正在尝试连接两个表并创建一个新的 Array 字段来保存某些查询的结果。

表一:

- id
- image
- difficulty
- answers (this is the new array field to create)

表 B:

- id (foreign key that references table a's id)
- name

我想将表 A 的答案数组的匹配 ID 的表 B 的“名称”字段放入。 我怎样才能完成这个/以及用什么查询?

编辑:我知道如何进行 JOIN。 我在问在执行 JOIN 时创建一个包含值数组的新列的方法是什么?

Select * from A, B where A.id = B.id

如果我理解正确,您需要一个标准(AKA 内部)连接,它只会显示表 a 和表 b 中都存在 id 的记录

SELECT a.id,
       a.image,
       a.difficulty,
       a.answers,
       b.name
FROM   a
       JOIN b
           ON a.id - b.id

或者,一个左连接将显示 a 中的所有记录和 b 中没有匹配 id 值的记录将为空

 SELECT a.id,
        a.image,
        a.difficulty,
        a.answers,
        b.name
  FROM  a
        LEFT JOIN b
            ON a.id - b.id

很难从提供的信息中确定您需要哪一个,但我认为这就是您想要的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM