我需要在一个SQL查询中更新两个表

时间:2019-01-07 19:13:43

标签: sqlite

在python中,我有以下代码:

cursor.execute("""
UPDATE customerDetails
SET customerDetails.cust_owed =
(customerDetails.cust_owed -orders.order_price),                                     
customerDetails.cust_paid = orders.order_price
WHERE orders.orderID = ?
AND orders.customerID = customerDetails.customerID""", (orderID,))

但是不管用,我们将不胜感激!

2 个答案:

答案 0 :(得分:0)

您需要进行两个单独的子选择才能使它起作用。

此外,您需要从要更新的列名中删除表名。

UPDATE customerDetails
SET    cust_owed = (cust_owed - (SELECT order_price FROM orders WHERE orders.orderID = ?)),
       cust_paid = (SELECT order_price FROM orders WHERE orders.orderID = ?)
WHERE customerID = (
    SELECT customerID FROM orders WHERE orderID = ? )


cursor.execute("""
  UPDATE customerDetails
  SET    cust_owed = (cust_owed - (SELECT order_price FROM orders WHERE orders.orderID = ?)),
         cust_paid = (SELECT order_price FROM orders WHERE orders.orderID = ?)
  WHERE customerID = (
      SELECT customerID FROM orders WHERE orderID = ? )""", (orderID,orderID,orderID))

答案 1 :(得分:-1)

据我所知,您可以用分号(;)分隔每个SQL查询。 例如:

Cursor.execute("""Update table 
(id,name) values ('1','test') where 
id=1;Update table (id,name) values 
('2','test2') where id=2;""")

我希望这会有所帮助。