mysql - 当所有表都没有主键时使用“SET”更新多列
问题描述
我有两个表如下。
表 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
-------------------------------------------
问题:ResultID
如果表 A 和表 B 之间匹配,我想得到
上面提到的结果表 A ,我想从表 B 中更新表 A 中的所有列ResultID
。如果ResultID
表 B 中的表 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 查询:) 快速而肮脏。
推荐阅读
- css - 为什么我的样式表外部链接不起作用?文中总结
- android - 带有 Google 身份验证的 IdentityServer 4
- sql - 如何查找正在阻塞其他进程的进程
- java - Gson 返回未知类型的 Arraylist
- python - cvxpy如何制定取决于决策变量符号的表达式
- c - 删除函数二叉搜索树 BST,其父传递树指针作为参数
- azure - botbuilder的自定义命令
- visual-studio - 打开新文件时默认net48是构建目标,如何将netcoreapp3.1设为默认?
- css - 关闭侧边栏与反应结合反应状态
- .net - 如何在 WCF 应用程序中隐藏初始化方法?