在SQL中使用连接进行更新的正确语法是什么?

时间:2011-06-16 20:23:20

标签: sql

我几分钟前发了一个问题并得到了这个问题

update pf
set price_test = (p.PRICE * .6)
from product as p
inner join product_featured as pf on pf.product_id = p.product_id

这是我的数据库结构

产品表

product_id  int(11) NO  PRI NULL    auto_increment
model   varchar(64) NO      NULL    
sku varchar(64) NO      NULL    
location    varchar(128)    NO      NULL    
quantity    int(4)  NO      0   
stock_status_id int(11) NO      NULL    
image   varchar(255)    YES     NULL    
manufacturer_id int(11) NO      NULL    
shipping    int(1)  NO      1   
price   decimal(15,4)   NO      0.0000

product_featured表

 product_id int(11) NO  PRI 0   
 price_test decimal(15,2)   YES     NULL

但这是我的错误

EDIT ..

我需要这个用于SQL .....

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 product as p
inner join product_featured as pf on pf.product_id = p.product' at line 3

4 个答案:

答案 0 :(得分:5)

试试这个。

update product_featured pf
set price_test = (
  select p.PRICE * .6
  from product p
  where pf.product_id = p.product_id
)

答案 1 :(得分:4)

我认为你将MySQL语法与MS SQL Server语法混淆,因为SQL Server支持UPDATE FROM而不支持MySQL; Jacob Egger用正确语法的答案似乎很好

答案 2 :(得分:3)

update sql语法为update ... set ... where ...。您将updateselect整合在一起......

答案 3 :(得分:1)

我认为以下内容适用于MySQL(但我知道你毕竟说过SQL Server)

UPDATE product AS p
   INNER JOIN product_featured AS pf ON pf.product_id=p.product_id
SET price_test = (p.price * .6)

我在SQL Server中收集了以下工作,但我没有这方面的工作:

UPDATE product_featured
    SET product_featured.price_test = (product.price * .6)
FROM product_featured
    INNER JOIN product  ON product.product_id = product_featured.product_id