Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 使用';设置';当所有表都没有主键时_Mysql_Sql_Database - Fatal编程技术网

Mysql 使用';设置';当所有表都没有主键时

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

我有两张桌子,如下所示

选项卡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
    -------------------------------------------
表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。这不能在单个查询中完成,并且需要在事务中进行两个单独的查询。从这篇评论中,您应该能够弄清楚,这两个步骤是什么。