breaker
breaker

Reputation: 41

select from table and update another table SQL

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?

Upvotes: 2

Views: 88

Answers (2)

AA.SC
AA.SC

Reputation: 377

Use simple "Inner join" in from clause to join both tables

UPDATE  bd
SET     rating = bd.rating + rvs.rating
FROM    reviews rvs
        INNER JOIN business_details bd ON db.business_id = rvs.business_id
WHERE   rvs.review_id = '$approveReviewID'

Upvotes: 1

Mureinik
Mureinik

Reputation: 312259

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'

Upvotes: 2

Related Questions