Mysql 使用';设置';当所有表都没有主键时
我有两张桌子,如下所示 选项卡A:Mysql 使用';设置';当所有表都没有主键时,mysql,sql,database,Mysql,Sql,Database,我有两张桌子,如下所示 选项卡A: ResultID | ImportDate | Comment1 ---------------------------------------------- 101 | 25-09-2019 | One in Table A ---------------------------------------------- 101 | 25-09-2019 | One copy i
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
-----------------------------------------------
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
-------------------------------------------
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
-------------------------------------------
表B:
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
-----------------------------------------------
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
-------------------------------------------
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:
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
-----------------------------------------------
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
-------------------------------------------
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,如果表A和表B之间匹配了ResultID
,我想更新表B中该ResultID
的表A中的所有列。如果表A中不存在表B中的ResultID
,请将其插入表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列和50000行
你能给我一些提示或解决办法吗?谢谢。你可以这样表演
SELECT CONCAT( "INSERT INTO ..... " )
FROM ....
如果这些表中不存在记录,则通过解析来生成SQL查询:)Quick and dirty。表中是否有其他数据值?否则,您如何决定
表B
中的哪一行应覆盖表a
中的给定行?@Nick表中也有其他数据值,但我想根据ResultId来决定。如果表B中的ResultId与表A中的ResultId匹配,请替换表A中该ResultId的所有列。如果不是,则将表B中带有ResultId的行添加到表AI。但是在TableA
和TableB
中,您有两个ResultId=101
值,那么您如何知道要用哪个值覆盖呢?是否有一个自动递增的id
字段可以用来对数据进行排序?如果没有主键,那么实际上就没有一个表,它会使任何类型的RDBM的问题脱离主题。如果我理解正确,您只需从表1中删除ResultId
的所有行,如果表2中存在该ResultId
。然后,您希望将该ResultId
的所有行从表2复制到表1。这不能在单个查询中完成,并且需要在事务中进行两个单独的查询。从这篇评论中,您应该能够弄清楚,这两个步骤是什么。