如何使用表Y更新表X中的StatusID?
表X有SourceID和old StatusID
表Y有SourceID和new StatusID

update x
set StatusID= (select StatusID from Y)
where
SourceID = (select SourceID from Y)

是这样吗?我不敢运行查询以防它把一切都搞砸。。。。
我正在使用joins获取表Y的StatusID,因此我认为需要使用SELECT
这就是我如何获取表Y的SourceID和StatusID
select  t2.Sourceid,  t3.ActionID
from tblSource t2
right join  Y t1 on t1.BaselineSourceKey= t2.tempSourceID
 right join lkuActionCode t3
       on t3.actioncode = CASE
       WHEN t1.actionCode  = 'R' THEN 'N'
       WHEN t1.actionCode  = 'B' THEN 'R'
       WHEN t1.actionCode  = 'A' THEN 'R'
       WHEN t1.actionCode  = 'E' THEN 'N'
       WHEN t1.actionCode  = 'F' THEN 'S'
       WHEN t1.actionCode  = 'G' THEN 'S'
       WHEN t1.actionCode  = 'K' THEN 'DP'
       WHEN t1.actionCode  = 'Q' THEN 'C'
       WHEN t1.actionCode  = 'S' THEN 'AER'
       WHEN t1.actionCode  = 'T' THEN 'AEN'
       WHEN t1.actionCode  = 'U' THEN 'C'
       WHEN t1.actionCode  = 'V' THEN 'UR'
    WHEN t1.actionCode  = 'W' THEN 'R'
        END
    where actionid <> 10 and actionid <> 8 and actionid <> 3

最佳答案

这可能更简单

update x
set StatusID= Y.StatusID
from Y
where y.SourceID = X.SourceID

如果是访问权限,那么您可以使用
update x inner join y on y.sourceid=x.sourceid
set x.statusid = y.statusid

关于mysql - 选择更新,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4673619/

10-10 14:29