【问题标题】:bulk update in SQLiteSQLite 中的批量更新
【发布时间】:2020-02-18 02:36:26
【问题描述】:

我有 2 个结构相同的表,我想使用另一个表中的数据更新一个表,并匹配主键。 SQLite 有一个 with (CTE) 语句,但以下不起作用(sqlite3 v. 3.29.0):

sqlite> select * from main;
1|A
2|B
4|D
5|E
6|F
sqlite> select * from temp;
1|aa
2|bb
3|cc
4|dd
5|ee
sqlite> with mapping as (select main.ID, temp.Desc from main join temp on temp.ID=main.ID) update main set Desc=mapping.Desc where main.ID=mapping.ID;
Error: no such column: mapping.Desc

我尝试使用“select main.ID as ID, temp.Desc as Desc”,但得到相同的错误消息。

【问题讨论】:

    标签: sqlite


    【解决方案1】:

    要从cte 更新主表,请使用subquery,因为sqlite 不支持update from

    with mapping as 
    (select main.ID, temp.Desc 
     from main 
     join temp on temp.ID=main.ID) 
    update main set Desc=
        (select Desc from mapping where ID = main.ID limit 1);
    

    dbfiddle

    【讨论】:

    • 我会试试这个,我怀疑它会让我做我需要做的事情。但是,我要指出我没有使用任何称为“更新自”的东西。根据文档,我可以在更新语句前加上 CTE,而我要做的就是在正常的更新语句中从该 CTE 中选择一个字段。对我来说仍然看起来像一个 SQLite 错误。但是,当然,感谢您提供的信息。
    猜你喜欢
    • 1970-01-01
    • 2020-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-10
    • 1970-01-01
    相关资源
    最近更新 更多