使用SQLite中的子查询通过添加来更新记录

时间:2017-02-18 17:15:06

标签: sqlite

我正在尝试通过在表DeliveryTetent中添加产品数量和名为DeliveryQuantity的记录来更新表StockCatalog中名为产品数量的记录,同时内部连接两个记录,StockCatalog.StockID = DeliveryContent.StockID内部加入DeliveryContent.DeliveryID = Deliveries。 DeliveryID。到目前为止,我有这个:

    UPDATE StockCatalog
    SET ProductQuantity = (SELECT StockCatalog.ProductQuantity FROM StockCatalog INNER JOIN DeliveryContent on StockCatalog.StockID = DeliveryContent.StockID WHERE StockCatalog.ProductQuantity + DeliveryContent.DeliveryQuantity)
    WHERE (SELECT DeliveryContent.DeliveryID FROM DeliveryContent INNER JOIN Deliveries on DeliveryContent.DeliveryID = Deliveries.DeliveryID)

但是,它似乎更新了StockCatalog中的所有ProductQuantity记录,并在DeliveryContent中更新了一个DeliveryQuantity记录。对不起如果这令人困惑。

1 个答案:

答案 0 :(得分:1)

似乎对WHERE的进展感到困惑。我认为这就是你想要的:

UPDATE StockCatalog sc SET ProductQuantity = ProductQuantity +
( SELECT DeliveryQuantity FROM DeliveryContent WHERE StockID=sc.StockID )
WHERE StockID in (select StockID from DeliveryContent);

如果每个StockID可以存在多个DeliveryContent,则可能会将SumQuantity替换为SUM(DeliveryQuantity)。