简体   繁体   English

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

[英]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_featured表

 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..... 我需要这个才能用于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

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; 我认为您将MySQL语法与MS SQL Server语法混淆了,因为UPDATE FROM在SQL Server中受支持,而在MySQL中不受支持; Jacob Egger's answer with the correct syntax seems nice 雅各布·艾格(Jacob Egger)用正确的语法回答似乎很好

The update sql syntax is update ... set ... where ... . update sql语法为update ... set ... where ... You're mangling update and select together... 您正在修改update然后一起select ...

I think the following works in MySQL (but I realise you said SQL Server after all) 我认为以下内容可在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)

I gather the following works in SQL Server, but I don't have that to hand: 我在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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM