Ho due tabelle: ITEMS
con quantità e unit_price (id | nome | order_id | qt | unit_price) e tavolo ORDERS
.MySQL UPDATE con SUM SELECT dalla tabella diversa
Voglio UPDATE
tabella orders
e inserire in orders.total_price sum of multiplications qt*unit_price
per gli stessi ordini per ottenere il prezzo totale dell'ordine.
La query SELECT
sulla tabella degli elementi è molto semplice e funziona bene giving somme per tutti gli elementi all'interno della stessa order_id:
SELECT SUM(items.qt*items.unit_price) from items GROUP by items.order_id
ma non posso inserire questo valore nella mia ORDERS
tavolo. Non ho potuto fare questo lavoro:
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
restituisce "Subquery returns more than 1 row"
ho trovato una domanda molto simile here ma la risposta non ha funzionato anche per me:
UPDATE orders SET orders.t_price = (SELECT SUM(items.qt*items.unit_price) from items WHERE orders.id = items.order_id)
per favore mostra la tua domanda completa cosa stai facendo come mysql_query ("query") –
Lo sto facendo per ora in phpmyadmin quindi la seconda query sopra è la query effettiva che provo: "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 " – Milosz