Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mikailfaruqali's avatar

Update Column value with subquery itself

hello everyone thanks for your replay i have average table and i want to update column based on previous rows. i was trying to do that with inner join but it not working

UPDATE `averages` 
SET cost = (SELECT SUM(qty*cost)/SUM(qty) FROM averages averages_was
WHERE averages_was.order_number < averages.order_number
AND averages_was.product_id = averages.product_id
AND averages_was.branch_id = averages.branch_id)
0 likes
24 replies
sr57's avatar

@mikailfaruqali

You use SUM (aggregate function) without GROUP BY

I suggest you to write /debug first your 'inner sql' and then write the update one.

sr57's avatar

@mikailfaruqali

Sorry @mikailfaruqali , I read your sql too quickly.

Yes you should be able to update the same table (with mysql & postgresql)

try changing

UPDATE `averages`  

to

UPDATE averages
sr57's avatar

@mikailfaruqali

Have you try this ?

UPDATE averages
SET cost = (SELECT SUM(qty*cost)/SUM(qty) FROM averages averages_was
WHERE averages_was.order_number < averages.order_number
AND averages_was.product_id = averages.product_id
AND averages_was.branch_id = averages.branch_id)
mikailfaruqali's avatar

@sr57 yes bro i tried this and give me this error

UPDATE averages
SET cost = (SELECT SUM(qty*cost)/SUM(qty) FROM averages averages_was
WHERE averages_was.order_number < averages.order_number
AND averages_was.product_id = averages.product_id
AND averages_was.branch_id = averages.branch_id)

error

SQLSTATE[HY000]: General error: 1093 You can't specify target table 'averages' for update in FROM clause (SQL: UPDATE averages SET cost = (SELECT SUM(qty*cost)/SUM(qty) FROM averages averages_was WHERE averages_was.order_number < averages.order_number AND averages_was.product_id = averages.product_id AND averages_was.branch_id = averages.branch_id))
sr57's avatar

@mikailfaruqali

Mine : mysql Ver 15.1 Distrib 10.3.34-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2

I'll send you a test to do on your db

1 like
sr57's avatar

@mikailfaruqali

Can you run this little test on your db

 CREATE TABLE test (product text,qty integer, cost float);
 UPDATE test SET cost = (SELECT SUM(qty*cost)/SUM(qty) FROM test t2 WHERE test.product=t2.product); 
mikailfaruqali's avatar

@sr57 bro run in phpmyadmin give this error

#1093 - You can't specify target table 'test' for update in FROM clause
mikailfaruqali's avatar

@sr57 sorry bro ,i run this query after updating version to 8.0.28-0ubuntu0.20.04.3 still same thing

#1093 - You can't specify target table 'test' for update in FROM clause

sr57's avatar

@mikailfaruqali

try this syntax (that's more correct -see my first answer)

UPDATE
    test t1 INNER JOIN (SELECT product,SUM(qty*cost)/SUM(qty) AS new_cost FROM test GROUP BY product) t2 ON
    (t1.product=t2.product)
SET t1.cost = t2.new_cost;
mikailfaruqali's avatar

@sr57 thanks bro befor post this question i tried INNER JOIN but this is not working fo me because i have more than one INNER JOIN and WHEN calculate each INNER JOIN have self condition so can get all record ,so i can't do it with INNER JOIN, i do this with laravel CHUNK() but it's too slow

mikailfaruqali's avatar

@sr57 yes bro it's worked ,sorry my english not good, bro i have two or more join in this table by type of record so each join have serval condition how i can do that ? one join not affects to another ,i used join only for SUM() of previous record

sr57's avatar

@mikailfaruqali

You are welcome, English is also not my mother language.

As I wrote before step after step, close this first question and update your second post your new point ... and we'll see this tomorow. Now it's time to sleep (for me)

Please or to participate in this conversation.