gpt4 book ai didi

mysql - 当所有表都没有主键时,使用 'SET' 进行多列更新

转载 作者:行者123 更新时间:2023-11-29 15:37:09 25 4
gpt4 key购买 nike

我有两个表,如下所示。

表A:

    ResultID     | ImportDate | Comment1
----------------------------------------------
101 | 25-09-2019 | One in Table A
----------------------------------------------
101 | 25-09-2019 | One copy in Table A
-----------------------------------------------
102 | 25-09-2019 | Two in Table A
-----------------------------------------------
103 | 25-09-2019 | Three in Table A
-----------------------------------------------

表B:

    ResultID     | ImportDate | Comment2
------------------------------------------
101 | 26-09-2019 | One in Table B
------------------------------------------
101 | 26-09-2019 | One copy in Table B
------------------------------------------
104 | 26-09-2019 | four in Table B
-------------------------------------------

所以输出应该是这样的

表A:

ResultID     | ImportDate | Comment1
---------------------------------------------
101 | 26-09-2019 | One in Table B
---------------------------------------------
101 | 26-09-2019 | One copy in Table B
--------------------------------------------
102 | 25-09-2019 | Two in Table A
--------------------------------------------
103 | 25-09-2019 | Three in Table A
---------------------------------------------
104 | 26-09-2019 | four in Table B
-------------------------------------------

问题:如果表 A 和表 B 之间的 ResultID 匹配,我想获得如上所述的结果表 A,我想从表 B 中更新表 A 中该 ResultID 的所有列。如果表 B 中的 ResultID 不存在于表 A 中,则将其插入到表 A 中。

注意: ResultId 不是两个表中的主键。

我在 MySQL 中尝试过的内容:如果 ResultId 是主键,则以下解决方案有效,但我想在 ResultID 不是主键时找到解决方案。

INSERT INTO TableA (ResultID, ImportDate, Comment1)
SELECT ResultID, ImportDate, Comment2 FROM TableB
ON DUPLICATE KEY UPDATE
ImportDate = VALUES(ImportDate),
Comment1 = VALUES(Comment1);

对于我的真实场景 - 我有 40 列和 50,000 行。

您能给我任何提示或解决方案吗?谢谢。

最佳答案

你可以这样表演。

SELECT CONCAT( "INSERT INTO ..... " )
FROM ....

如果这些表中不存在记录,则解析生成 SQL 查询的方式:) 快速而肮脏。

关于mysql - 当所有表都没有主键时,使用 'SET' 进行多列更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58127164/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com