【问题标题】:UPDATE a field with a value from another table用另一个表中的值更新一个字段
【发布时间】:2016-04-26 03:13:23
【问题描述】:

我需要使用 Python 连接器使用 MySQL 中另一个表中的值更新一个字段(虽然不是那么重要)。我需要根据匹配条件从一个表中选择一个值,然后根据相同的匹配条件将提取的列插入到上一个表中。

我有以下内容,这当然不起作用。

for match_field in list:
        cursor_importer.execute(UPDATE table1 SET table1_field = 
             (SELECT field_new FROM table2 WHERE match_field = %s) 
             WHERE match_field = %s LIMIT 1,
             (match_field, match_field ))

【问题讨论】:

    标签: python mysql mysql-connector


    【解决方案1】:

    您可以将UPDATEJOINS 一起使用。

    以下是 MySQL 中的示例:

    UPDATE table1 a JOIN table2 b ON a.match_field = b.match_field
    SET a.table1_field = b.field_new
    WHERE a.match_field = 'filter criteria'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-06
      • 1970-01-01
      • 2015-10-03
      • 2011-02-15
      • 1970-01-01
      • 2013-03-03
      • 1970-01-01
      相关资源
      最近更新 更多