开发者

how to select command with in update command

i have three table

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

i want to update table 't1' and set t1.sVal="" where t2.Pid='a' and t3.MValue='zzzz'

how can i do this pls help me thanks in advance

i have tried it like

update t1 set sVal="" where开发者_运维问答 Sid=(select Sid from t2 where Pid='a' and Mid=(select Mid from t3 where MVale='ZZZZ'))

but it doesnt work and thorw error like

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. 


The problem is that you are using double quotes instead of single quotes in your set statement.

set sVal=''

not set sVal=""

But if you want a cleaner update statement, this code should do it.

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'


Try this:

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'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜