Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql 如何连接两个表并创建新列?_Postgresql - Fatal编程技术网

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

Postgresql 如何连接两个表并创建新列?,postgresql,Postgresql,使用PostgreSQL 10.4 我正在尝试连接两个表并创建一个新的数组字段,该字段将保存某个查询的结果 表A: - id - image - difficulty - answers (this is the new array field to create) 表B: - id (foreign key that references table a's id) - name 我想把表B的'name'字段放在表A的答案数组的匹配ID中。我如何完成这个/以及使用什么查询 编辑:我知道如何

使用PostgreSQL 10.4

我正在尝试连接两个表并创建一个新的数组字段,该字段将保存某个查询的结果

表A:

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

- id (foreign key that references table a's id)
- name
我想把表B的'name'字段放在表A的答案数组的匹配ID中。我如何完成这个/以及使用什么查询


编辑:我知道如何加入。我想问的是,如何创建一个新列,在执行联接时保存一个值数组?

从a,B中选择*其中a.id=B.id
如果我理解正确,您需要一个标准(又称内部)联接 它将只显示id同时存在于表a和表b中的记录

SELECT a.id,
       a.image,
       a.difficulty,
       a.answers,
       b.name
FROM   a
       JOIN b
           ON a.id - b.id
或者,显示a中所有记录和b中没有匹配id值的记录的左连接将为null

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

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

,虽然这段代码可能会回答这个问题,但提供关于如何和/或为什么解决问题的附加上下文将提高答案的长期价值。