Home > DeveloperSection > Forums > select record one table and update another table
Pravesh Singh
Pravesh Singh

Total Post:101

Posted on    November-06-2014 3:42 AM

 MSSQL Server SQL  SQL Server 2012 

 1 Reply(s)
 1076  View(s)
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?

jacob rasel
jacob rasel

Total Post:88

Posted on    November-06-2014 6:29 AM

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'

Don't want to miss updates? Please click the below button!

Follow MindStick