Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server - inner join when updating [duplicate]

I have the below query which does not work. What am I doing wrong? Is this even possible?

UPDATE ProductReviews AS R     INNER JOIN products AS P         ON R.pid = P.id  SET R.status = '0'  WHERE R.id = '17190'    AND P.shopkeeper = '89137' 
like image 410
LeeTee Avatar asked Mar 06 '12 17:03

LeeTee


People also ask

Can we use inner join in update statement?

SQL Server UPDATE JOIN syntax To query data from related tables, you often use the join clauses, either inner join or left join. In SQL Server, you can use these join clauses in the UPDATE statement to perform a cross-table update.

How do I remove duplicates inner join SQL?

Solution. Select column values in a specific order within rows to make rows with duplicate sets of values identical. Then you can use SELECT DISTINCT to remove duplicates. Alternatively, retrieve rows in such a way that near-duplicates are not even selected.

Can Joins be used to update?

SQL UPDATE JOIN could be used to update one table using another table and join condition.


2 Answers

UPDATE R  SET R.status = '0'  FROM dbo.ProductReviews AS R INNER JOIN dbo.products AS P         ON R.pid = P.id  WHERE R.id = '17190'    AND P.shopkeeper = '89137'; 
like image 121
Aaron Bertrand Avatar answered Sep 19 '22 08:09

Aaron Bertrand


This should do it:

UPDATE ProductReviews SET    ProductReviews.status = '0' FROM   ProductReviews        INNER JOIN products          ON ProductReviews.pid = products.id WHERE  ProductReviews.id = '17190'        AND products.shopkeeper = '89137' 
like image 34
Bridge Avatar answered Sep 20 '22 08:09

Bridge