php - How can I update multiple mysql columns of a row using the result of a select query?


I have a reviews table that contains three ways to rate an item. The items themselves then have three columns to hold the average for each value respectively.

I could do this using three nested queries in an update query, but I feel like this is inefficient... Is there a way to update them all at once?

So far I've used this as my select query:

SELECT AVG(rating_1),AVG(rating_2),AVG(rating_3) FROM items_reviews WHERE item_id = 1

I just don't know how to use the result of that query to update an item row.




You could use an join in theUPDATE:

UPDATE items a
        AVG(rating_1) AS avg1, 
        AVG(rating_2) AS avg2, 
        AVG(rating_3) AS avg3
    FROM items_reviews
    WHERE item_id = 1
    GROUP BY item_id
) b ON a.item_id = b.item_id
    a.avgrating1 = b.avg1,
    a.avgrating2 = b.avg2,
    a.avgrating3 = b.avg3

People are also looking for solutions to the problem: Facebook PHP SDK Connection


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.