SQL UPDATE 与链接表混淆(子查询返回超过 1 个值)
数据库有一个表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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)