【问题标题】:How do I update a Column in SQL from one table using a different table with SQL Alchemy?如何使用 SQL Alchemy 使用不同的表从一个表更新 SQL 中的列?
【发布时间】:2021-03-03 17:46:57
【问题描述】:

我有 2 个表格,table1 是一个正在运行的库存清单,table2 是当天更新的销售额。我将如何更新表 Sold 字段以反映表 1 库存中的更新?我尝试使用:

FROM table1 INNER JOIN table2 ON table1.SKU = table2.SKU ```

I'm using the Python module: SQL Alchemy to do this, and am getting "(sqlite3.OperationalError) near ".": syntax error".

Any ideas?

【问题讨论】:

  • 如果我的解决方案回答了您的问题,请将其标记为已接受以关闭它。如果问题仍然存在,请发布更新,我会尽力提供帮助

标签: python mysql sqlite sqlalchemy


【解决方案1】:

这是一个 sqlite 错误;与 Python 或 SQLAlchemy 无关

你把你的 sqlite 语法搞混了;您需要使用子查询而不是联接

请参阅文档here 中的第 2.2 节

UPDATE table1
SET Sold = table1.Sold +
(
    SELECT Sold
    FROM table2
    WHERE table1.SKU = table2.SKU
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-17
    • 1970-01-01
    • 1970-01-01
    • 2014-12-31
    • 1970-01-01
    • 2019-08-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多