使用选择更新

发布于 2024-10-11 13:35:50 字数 1225 浏览 2 评论 0原文

如何使用表 Y 更新表 X 中的 StatusID?

表 X 具有 SourceID 和旧 StatusID
表 Y 有 SourceID 和新的 StatusID

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

这是对的吗?我害怕运行查询,以防它把一切搞乱......

我正在使用联接来获取表 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

How can I update StatusID in Table X using Table Y?

Table X has SourceID and old StatusID
Table Y has SourceID and new StatusID

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

Is this right? I'm afraid to run the query in case it messes everything up....

I am using joins to get the StatusID for table Y, so I think I need to use a SELECT.

This is how I'm getting SourceID and StatusID for table Y

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

画▽骨i 2024-10-18 13:35:50

这可能会更简单

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

如果是 Access,那么您可以使用

update x inner join y on y.sourceid=x.sourceid
set x.statusid = y.statusid

This could be simpler

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

If it is Access, then you could use

update x inner join y on y.sourceid=x.sourceid
set x.statusid = y.statusid
開玄 2024-10-18 13:35:50

我不确定这是否有效。尝试:

update x set StatusID=Y.StatusID
from Y where (x.SourceID=Y.SourceID);

ETA:这应该在 PostgreSQL 中工作,但我不确定其他 SQL 方言。

I'm not sure that works. Try:

update x set StatusID=Y.StatusID
from Y where (x.SourceID=Y.SourceID);

ETA: This should work in PostgreSQL, but I'm not sure about other SQL dialects.

习惯成性 2024-10-18 13:35:50
update x
set StatusID = y.StatusID
from x
join y on x.SourceID= y.SourceID
update x
set StatusID = y.StatusID
from x
join y on x.SourceID= y.SourceID
笑叹一世浮沉 2024-10-18 13:35:50
update x, y 
  set x.StatusID=y.StatusID 
  where x.SourceID=y.SourceID
update x, y 
  set x.StatusID=y.StatusID 
  where x.SourceID=y.SourceID
柠栀 2024-10-18 13:35:50

已更新
在 SQL Server 中,您可以这样做:

UPDATE A
SET A.StatusID= B.StatusId
FROM TableX AS A
JOIN TableY AS B
ON A.SourceID = B.SourceID

在更新的问题中,现在您只是执行 SELECT,它根本不会更新任何记录。您使用什么数据库引擎?

UPDATED
In SQL Server you can do this:

UPDATE A
SET A.StatusID= B.StatusId
FROM TableX AS A
JOIN TableY AS B
ON A.SourceID = B.SourceID

In your updated question, now you are just doing a SELECT, it's not gonna update any record at all. What database enginge are you using?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文