1

我正在尝试使用此查询

UPDATE products  
SET products.product_price = '87.00000'
FROM products
INNER JOIN product_category
ON products.product_id = product_category.product_id 
WHERE product_category.category_id = '64'

但是我收到此错误:#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM table products INNER JOIN table product_category ON prod' at line 3

我没有看到任何语法错误。我从这个论坛上的例子中提出了这个问题。

4

4 回答 4

3

删除FROM products线。并将该 SET ...行放在WHERE子句之前。

要清楚:

UPDATE ...
JOIN ...
SET ...
WHERE ...

你也可以

UPDATE products p
SET p.product_price='87.00000'
WHERE EXISTS (SELECT NULL 
              FROM product_category pc
              WHERE p.product_id = pc.product_id
              AND pc.category_id = '64');
于 2013-01-23T23:05:55.960 回答
1

您可以使用以下内容:

UPDATE products 
INNER JOIN product_category
  ON products.product_id = product_category.product_id 
SET products.product_price = '87.00000'
WHERE product_category.category_id = '64';

请参阅带有演示的 SQL Fiddle

于 2013-01-23T23:08:53.360 回答
1

我认为 SET 子句需要在表引用之后和 WHERE 子句之前,如下所示:

UPDATE products
 INNER
  JOIN product_category
    ON products.product_id = product_category.product_id 
   SET products.product_price = '87.00000'
 WHERE product_category.category_id = '64'

当我需要这样的 UPDATE 时,这就是我获得该语法的方法。我总是从一个 select 语句开始,它让我知道哪些行将被更新。例如:

SELECT p.*, c.*
  FROM products p
  JOIN product_category c
    ON p.product_id = c.product_id 
 WHERE c.category_id = '64'

要将其转换为更新语句,我SET在表引用之后和子句之前添加了一个“”子句WHERE,然后将“ SELECT ... FROM”替换为“ UPDATE”关键字。瞧。

于 2013-01-23T23:09:26.923 回答
1
UPDATE products
INNER JOIN product_category
ON products.product_id = product_category.product_id 
SET products.product_price = '87.00000'
WHERE product_category.category_id = '64'
于 2013-01-23T23:09:40.373 回答