首页 > 解决方案 > MySQL 中带有“SET”且没有主键但条目相同的两个表之间的多列更新

问题描述

我有两个表如下。

表 A:

    ResultID(PK) | ImportDate | Comment1
    -------------------------------------
    101          | 25-09-2019 | One
    101          | 25-09-2019 | Two
    103          | 25-09-2019 | Three
    103          | 25-09-2019 | Four
    ----------------------------------------

表 B:

    ResultID(PK) | ImportDate | Comment2
    -------------------------------------
    101          | 26-09-2019 | new one
    101          | 26-09-2019 | new two
    104          | 26-09-2019 | new three
    104          | 26-09-2019 | new four
    --------------------------------------

所以输出应该看起来像

表 A:

ResultID(PK) | ImportDate | Comment1
-------------------------------------
101          | 26-09-2019 | new one
101          | 26-09-2019 | new two
103          | 25-09-2019 | Three
103          | 25-09-2019 | four
104          | 26-09-2019 | new three
104          | 26-09-2019 | new four
--------------------------------------

问题:如果 ResultID 在表 A 和表 B 之间匹配,我想得到上面提到的结果表 A,我想从表 B 中为该 ResultID 更新表 A 中的所有列。如果表 B 中的 ResultID 不存在于表 A 中,则将其插入表 A。并且它没有主键。并且条目将是唯一的,例如特定 ResultID 的 2 个条目。

标签: phpmysqlarrays

解决方案


推荐阅读