【问题标题】:In SQLite, how can I update a column in TABLE A with the values from a column in TABLE B?在 SQLite 中,如何使用表 B 中的列的值更新表 A 中的列?
【发布时间】:2011-05-04 01:11:56
【问题描述】:

需要帮助,因为我运气不好。

表A

id   groupid   
 1     100   
 2     101   
 3     102  

表 B

groupid   newid  
 100        100  
 101        100   
 102        100 

更新表 A,使表 A 变为

id   groupid   
 1     100   
 2     100   
 3     100  

它使用 TableB 来获取 newid。

提前致谢

【问题讨论】:

    标签: sql sqlite sql-update


    【解决方案1】:

    sqlite 不支持更新中的连接,但您可以使用子查询。试试这样的:

    update a
    set groupid = coalesce(
     (select newid from b where groupid = a.groupid limit 1),
     groupid
    );
    

    【讨论】:

    • 如果您知道 b 中的每一行恰好对应 a 中的一行,那么限制 1 和合并应该是不必要的。但安全总比后悔好。
    猜你喜欢
    • 2014-05-10
    • 1970-01-01
    • 2020-02-19
    • 2021-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-01
    相关资源
    最近更新 更多