将记录值从antoher表更改为其id

时间:2016-02-24 15:18:06

标签: sql postgresql

我有一张桌子A和B.表A得到了:

Id | artist | artist_id(that is empty now)
--------------------------------------------
1  | John   |
2  | Jack   |

表B有:

artist_id | artist
-------------------
34        | John
56        | Jack
57        | Mike

我想在表A中设置artist_id,其中艺术家与B中的相同。 结果将是:

Id | artist | artist_id
--------------------------------------------
1  | John   | 34
2  | Jack   | 56

如何在postgresql中执行此操作?

3 个答案:

答案 0 :(得分:2)

这应该回答,使用UPDATE yourtable A和FROM表来加入(B)

UPDATE TABLEA AS A 
SET A.artist_id = B.artist_id
FROM TABLEB as B
WHERE A.artist = B.artist

答案 1 :(得分:1)

从名称切换到ID是个好主意。假设您没有重复项,可以使用from子句或相关子查询轻松完成:

update a
    set artist_id = b.artist_id
    from b
    where a.name = b.name;

答案 2 :(得分:0)

以下查询可以正常运行,假设artist_id tableB中每个UPDATE tableA SET artist_id = tableB.artist_id FROM tableB WHERE tableA.artist = tableB.artist AND tableA.artist_id IS NULL 有1行(唯一值)。

listeners: {
  blur: function(combo) {
    if (combo.queryFilter) {
      combo.queryFilter.setValue('');
      combo.getStore().filter();
    }
  }
}
相关问题