2011-06-20 106 views
1

選擇命令我有三個表如何更新命令

table t1; 
Sid sValue 
1 abc 
2 bcd 

table t2 
Sid Pid Mid 
1 a 9 
2 a 10 
3 b 9 

table t3 
Mid MValue 
9 ZZZZ 
10 yyyy 

我想更新表「T1」和 集t1.sVal =「」其中t2.Pid =「a」和T3。 MValue = 'ZZZZ'

我怎麼能做到這一點請幫助我在此先感謝

我已經嘗試過了類似

update t1 set sVal="" where Sid=(select Sid from t2 where Pid='a' and Mid=(select Mid from t3 where MVale='ZZZZ')) 

但它不工作,並投擲攻擊錯誤 像

An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Add a name or single space as the alias name. 

回答

2

的問題是,你正在使用雙引號代替單引號在您所設定的語句。

集SVAL = ''

沒有設置SVAL = 「」

但是,如果你想有一個更清潔的更新語句,此代碼應該這樣做。

update t1 set sVal='' 
from t1 
inner join t2 on t1.sid=t2.sid 
inner join t3 on t2.Mid=t3.Mid 
where t2.Pid='a' and t3.MVale='ZZZZ' 
+0

喔......用於校正和簡單的答案謝謝 – sanu

0

試試這個:

update t1 SET t1.sVal = '' 
INNER JOIN t2 
ON t1.sid = t2.sid 
INNER JOIN t3 
ON t2.mid = t3.mid 
WHERE t2.Pid='a' and t3.MValue='zzzz'