2010-04-28 17 views
1

我在制定法律聲明時遇到了麻煩,使已發貨(sp)超過500個單位的供應商的狀態加倍。 我一直想:使用SQL中的更新中的JOIN加入

update s 
set s.status = s.status * 2 
from s join sp 
    on (sp.sno = s.sno) 
group by sno 
having sum(qty) > 500; 

但是我從MySQL得到這個錯誤:

ERROR 1064 (42000): 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 s join sp on (sp.sno = s.sno) group by sno having sum(qty) > 500' at line 1 

有沒有人有什麼錯此查詢任何想法?這是我的模式:

create table s 
    (sno char(5) not null, 
    sname char(20) not null, 
    status smallint, 
    city char(15), 
    primary key (sno) 
); 
create table p 
    (pno char(6) not null, 
    pname char(20) not null, 
    color char(6), 
    weight smallint, 
    city char(15), 
    primary key (pno) 
); 
create table sp 
    (sno char(5) not null, 
    pno char(6) not null, 
    qty integer not null, 
    primary key (sno, pno) 
); 
+1

該組可能會導致該問題。 – 2010-04-28 03:09:08

+0

有沒有其他方法可以得到與沒有group by子句的having子句相同的效果? – SDLFunTimes 2010-04-28 03:11:30

回答

2

正式地,ANSI SQL不支持UPDATE子句中的FROM子句。這是供應商特定的功能。相反,您可以執行如下操作:

Update s 
Set status = status * 2 
Where Exists (
       Select 1 
       From sp 
       Where sp.sno = s.sno 
       Group By sp.sno 
       Having Sum(qty) > 500 
       ) 
+0

非常感謝。問題解決了。 – SDLFunTimes 2010-04-28 03:14:54

0

標準SQL具有允許基於連接進行更新的MERGE。除了在SET子句後面使用子查詢之外,標準SQL UPDATE中不允許連接。

+0

我得看看使用MERGE。最初我嘗試了一個子查詢,但需要查詢正在更新的表。 – SDLFunTimes 2010-04-28 21:14:59