在SQL中加入更新的正确语法是什么?

问题描述:

我张贴的问题在几分钟前,并得到了这个查询在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 

但这是我的错误

编辑..

我需要这对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 
+0

你是什么意思,你需要这个工作的SQL?你的错误消息说:“.. MySQL服务器版本...” – 2011-06-16 20:45:28

+0

如果您必须拥有ANSI SQL,看起来像@Jacob Eggers的答案是您想要的答案。 – 2011-06-16 20:54:48

试试这个。

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

+1 [根据Hugo Kornelis](http://sqlblog.com/blogs/hugo_kornelis/archive/2008/03/10/lets-deprecate-update-from .aspx),这实际上是执行这种更新的ANSI SQL标准方法。出发,OP,并得到满足。 :) – 2011-06-16 20:53:13

update SQL语法是update ... set ... where ...。你正在将updateselect拼在一起...

+0

语法正确,但不支持MySQL。 – Guffa 2011-06-16 20:43:53

+0

实际上,更新... select可以用于Oracle – jclozano 2016-02-11 17:46:53

我想既然更新来自SQL Server中,而不是在MySQL支持你的MySQL困惑与语法MS SQL Server语法;雅各艾格与正确的语法答案似乎不错

+0

我需要这个SQL不是mysql – Trace 2011-06-16 20:42:14

+0

@Guffa我需要这个用于SQL而不是mysql ...我在这个问题上的错误 – Trace 2011-06-16 20:43:35

+0

@Tamer你的问题被标记为MySQL ...并且,正如这个答案所示,没有绝对的 - 正确的“SQL”语法;不同的实现有自己的。 – 2011-06-16 20:43:52

我想在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