我对此脚本有疑问:
UPDATE Orders AS o
SET o.menuId = 14259
WHERE o.menuId = 14422
AND o.userId =
(SELECT Id FROM users where Group =
(SELECT Id FROM groups where eatGroupId = 4))
错误:子查询返回超过1行//因为有许多组和用户。有谁知道它是否有可能使这个查询有效?或其他替代方案?
答案 0 :(得分:0)
{{1}}
答案 1 :(得分:0)
修改您的查询以更新第4组下的所有用户:
UPDATE Orders AS o
SET o.menuId = 14259
WHERE o.menuId = 14422
AND o.userId IN
(SELECT Id FROM users where Group =
(SELECT Id FROM groups where eatGroupId = 4))
或更新前1行使用:
UPDATE Orders AS o
SET o.menuId = 14259
WHERE o.menuId = 14422
AND o.userId =
(SELECT TOP 1 Id FROM users where Group =
(SELECT TOP 1 Id FROM groups where eatGroupId = 4))
答案 2 :(得分:0)
使用IN
运算符,该运算符允许WHERE
cluase中的多个值并执行子查询中的JOIN
UPDATE Orders
SET menuId = 14259
WHERE menuId = 14422 AND
userId IN (
SELECT u.Id FROM users u
INNER JOIN groups g ON g.Id= u.Group
WHERE g.eatGroupId = 4)
答案 3 :(得分:0)
您可以尝试使用此代码
UPDATE Orders AS o
SET o.menuId = 14259
WHERE o.menuId = 14422
AND o.userId =
(SELECT Id FROM users where Group =
(SELECT Id FROM groups where eatGroupId = 4 group by Id) group by Id)
答案 4 :(得分:0)
谢谢大家的回答。我希望它会对某人有所帮助。
UPDATE Orders AS o
JOIN users u on u.Id=o.userId
JOIN groups g on g.Id=u.Group
SET o.menuId = 14259
WHERE o.menuId = 14422
AND g.eatGroupId = 4;