437

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'
2
  • UPDATE dbo.ProductReviews AS R SET R.status = '0' INNER JOIN dbo.products AS P ON R.pid = P.id WHERE R.id = '17190' AND P.shopkeeper = '89137';
    – Hassan
    Aug 24, 2014 at 13:05
  • 2
    This is not a duplicate because he is not trying to update one table FROM another. He is only joining to enforce the where clause.
    – nuander
    Oct 2, 2017 at 22:41

2 Answers 2

921
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';
2
  • can you please explain why syntax alike UPDATE JOIN SET WHERE is incorrect, but it works with UDPATE SET FROM JOIN WHERE? Does it depend on the version of mysql?
    – Farside
    Apr 28, 2016 at 12:45
  • 14
    @Farside this syntax is for SQL Server. Someone else will have to help you with MySQL syntax, sorry. Apr 28, 2016 at 13:37
75

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'
3
  • 6
    this gives me ERROR: table name "ProductReviews" specified more than once in postgresql Apr 26, 2017 at 16:54
  • 1
    fixed it by doing FROM ProductReviews AS pr also SET status = '0' instead of ProductReviews.status = '0' Apr 26, 2017 at 17:03
  • 10
    @JohhanSantana This question was tagged SQL server - no guarantee it'll work on other DBMS systems.
    – Bridge
    Apr 28, 2017 at 8:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.