1

i am seeking help on updating one column in a table with results from another query.

table names:

saleitems - fields are (itemid,quantity,saledate) 
stockbuffers - fields are (itemid,bufferquantity)

i am getting the buffer quantity with the following query from sale items table:

SELECT (SUM(saleitems.quantity / 6)) * 6 FROM saleitems WHERE saleitems.saledate BETWEEN date_sub(curdate(),interval 180 day) AND NOW() GROUP BY saleitems.itemid

i want to update the column buffer quantity from the above query's result. can anyone help. greatly appreaciate.

0

1 Answer 1

1

You can use update query as:

Update stockbuffers set bufferquantity =(SELECT (SUM(saleitems.quantity / 6)) * 6 FROM saleitems WHERE saleitems.saledate BETWEEN date_sub(curdate(),interval 180 day) AND NOW() GROUP BY saleitems.itemid)

its just a sample and not have been tested.

2
  • how would the query know which row to update since there are two tables. where do i place the join query to join the tables since itemid is a common field in both tables
    – mmdel
    Jul 28, 2012 at 14:34
  • The query will be updating the stockbuffer table as we have specified in the code as "Update stockbuffers" Jul 28, 2012 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.