开发者

What's the correct syntax for an update with a join in SQL?

I posted a question a few minutes ago and got this query

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

here is my db structure

product table

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

the product_featured table

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

but here is my error

EDIT..

I need this to work for SQL.....

You hav开发者_如何学Pythone 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


Try this.

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


I think you are confusing MySQL syntax with MS SQL Server syntax since UPDATE FROM is supported in SQL Server and not in MySQL; Jacob Egger's answer with the correct syntax seems nice


The update sql syntax is update ... set ... where .... You're mangling update and select together...


I think the following works in MySQL (but I realise you said SQL Server after all)

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

I gather the following works in SQL Server, but I don't have that to hand:

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜