Sqlite 我需要在一个SQL查询中更新两个表
在python中,我有以下代码:Sqlite 我需要在一个SQL查询中更新两个表,sqlite,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 = ? A
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,))
无论它如何不起作用,任何帮助都将不胜感激 正如我所知,您可以用分号(;)分隔每个SQL查询。 例如:
Cursor.execute("""Update table
(id,name) values ('1','test') where
id=1;Update table (id,name) values
('2','test2') where id=2;""")
我希望这会有所帮助。您需要做出两个单独的子选择,以使这项工作正常 此外,还需要从正在更新的列名中删除表名
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))
只需在一个事务中进行两次更新。@Shawn,他的SQL至少有5个主要问题。我认为他需要更多的指导……如果你读过实际的查询,你就会意识到他实际上想利用两个表更新两列。实际上并没有更新两个表。这个答案没有回答他的问题