SQL UPDATE 与链接表混淆(子查询返回超过 1 个值)

发布于 2024-11-15 00:38:42 字数 570 浏览 4 评论 0原文

数据库有一个表A,表BAID与B.ID链接。

我需要将 A.Name 的所有实例更新为等于 B.Catalog,当且仅当这些实例链接到 A.ID = B.ID 时。

仅应更新 A 中 A.Owner = 0 的条目。

我想出的代码是:

UPDATE A
SET A.Name = (SELECT B.Catalog
    FROM B, A
    WHERE (B.ID = A.ID) AND (A.Owner = 0))
WHERE A.ID IN (SELECT B.ID
    FROM B, A
    WHERE (B.ID = A.ID) AND (A.Owner = 0))

错误是“子查询返回多个值”(MS SQL 2005添加了“MSG 512,LEVEL 16,STATE 1,LINE 1”)。我明白这个错误告诉我什么,我试图将单个值设置为等于 SELECT 语句返回的多个结果,但我认为我对 SQL 的了解太缺乏,无法提出正确的代码。

任何帮助将不胜感激。这就是我尝试自学所得到的结果!头疼!

Database has a table A, and a table B. A.ID is linked with B.ID.

I need to update all instances of A.Name to be equal to B.Catalog, if and only if those instances are linked A.ID = B.ID.

Only entries in A where A.Owner = 0 should be updated.

The code I have come up with is:

UPDATE A
SET A.Name = (SELECT B.Catalog
    FROM B, A
    WHERE (B.ID = A.ID) AND (A.Owner = 0))
WHERE A.ID IN (SELECT B.ID
    FROM B, A
    WHERE (B.ID = A.ID) AND (A.Owner = 0))

The error is "Subquery returned more than one value" (MS SQL 2005 adds "MSG 512, LEVEL 16, STATE 1, LINE 1"). I understand what the error is telling me, that I am trying to set a single value to be equal to the multiple results returned by the SELECT statement, but I think my knowledge of SQL is too lacking to come up with the proper code.

Any help would be greatly appreciated. This is what I get for trying to teach myself! A headache!

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

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

发布评论

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

评论(2

-柠檬树下少年和吉他 2024-11-22 00:38:42
update A
set A.Name = B.Catalog
from TableA A
    join TableB B on
        A.Id = B.Id
where A.Owner = 0
update A
set A.Name = B.Catalog
from TableA A
    join TableB B on
        A.Id = B.Id
where A.Owner = 0
带上头具痛哭 2024-11-22 00:38:42
UPDATE A
SET A.Name = B.Catalog
FROM A INNER JOIN B ON A.ID = B.ID
WHERE A.Owner = 0
UPDATE A
SET A.Name = B.Catalog
FROM A INNER JOIN B ON A.ID = B.ID
WHERE A.Owner = 0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文