SELECT RECORD ONE TABLE AND UPDATE ANOTHER TABLE

Pravesh Singh

Total Post:101

Points:709
Posted by  Pravesh Singh
 1124  View(s)
Ratings:
Rate this:

i'm trying to get 'business_id' and 'rating' from the table 'reviews' where 'review_id' is $approveReviewID

and then update 'rating' in 'business_details' with this retrieved rating from 'reviews' where 'business_id' is equal to that retrieved.

i have these two queries,

SELECT business_id, rating FROM reviews WHERE review_id = '$approveReviewID'

UPDATE business_details SET rating = rating + $rating WHERE business_id = '$businessID

is there a way to join these queries into one?

  1. jacob rasel

    Post:88

    Points:616
    Re: select record one table and update another table

    Yes, you can use the update-join syntax:

    UPDATE business_details bd
    JOIN   reviews r ON bd.business_id = r.business_id
    SET    bd.rating = bd.rating + r.rating
    WHERE  r.review_id = '$approveReviewID'

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!