【问题标题】:How can i update column data on two columns match condition in SQLITE?如何更新 SQLITE 中两列匹配条件的列数据?
【发布时间】:2020-05-23 04:36:59
【问题描述】:

我有 2 个表 table1 (col1, col2, col3, col4) 和 table2 (çol1, col2, col5)。

Table1 col3 为空,我想根据 table1(col1, col2) 和 table2 (col1, col2) 的匹配更新 table2 col5 中的数据。

【问题讨论】:

    标签: mysql sqlite


    【解决方案1】:

    对于 SQLite:

    UPDATE table1
    SET col3 = ( SELECT col5
                 FROM table2
                 WHERE (table1.col1, table1.col2)=(table2.col1, table2.col2) 
                 LIMIT 1 )
    /* WHERE table1.col3 IS NULL */
    

    如果table2 中的(col1, col2) 被定义为唯一,则可以删除LIMIT 1


    对于 MySQL(问题是 MySQL 标记的):

    UPDATE table1
      JOIN table2 USING (col1, col2)
    SET table1.col3 = table2.col5
    /* WHERE table1.col3 IS NULL */
    

    table2 中的(col1, col2) 必须定义为唯一的。如果不是,那么将使用所有可能的随机值进行更新。

    【讨论】:

    • 非常感谢,在我的 SQLITE 数据库中工作正常。
    猜你喜欢
    • 1970-01-01
    • 2013-10-28
    • 2018-01-02
    • 2020-06-25
    • 2022-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多