在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
答
试试这个。
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
答
我想在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
你是什么意思,你需要这个工作的SQL?你的错误消息说:“.. MySQL服务器版本...” – 2011-06-16 20:45:28
如果您必须拥有ANSI SQL,看起来像@Jacob Eggers的答案是您想要的答案。 – 2011-06-16 20:54:48