我有两个表: ITEMS数量和unit_price(id |名称| order_id | qt | unit_price)和table ORDERS。
ITEMS
ORDERS
我想对相同的订单进行UPDATE表orders和放置,orders.total_price sum of multiplications qt*unit_price以获取订单的总价。
UPDATE
orders
orders.total_price sum of multiplications qt*unit_price
该SELECT上的项目表查询很简单,适用于相同的order_id内的所有项目给予罚款数额:
SELECT
SELECT SUM(items.qt*items.unit_price) from items GROUP by items.order_id
但我无法在ORDERS表中插入此值。我无法完成这项工作:
UPDATE orders, items SET orders.total_price = (SELECT SUM(items.qt*items.unit_price) FROM items GROUP BY items.order_id) WHERE orders.id = items.order_id
它返回 "Subquery returns more than 1 row"
"Subquery returns more than 1 row"
我在这里找到了一个非常类似的问题,但答案对我也没有用:
UPDATE orders SET orders.t_price = (SELECT SUM(items.qt*items.unit_price) from items WHERE orders.id = items.order_id)
您可以UPDATE与JOIN荷兰国际集团的两个表:
JOIN
UPDATE Orders o INNER JOIN ( SELECT order_id, SUM(qt * unit_price) 'sumu' FROM items GROUP BY order_id ) i ON o.id = i.order_id SET o.total_price = i.sumu [WHERE predicate]