使用子查询更新多行
我有两个表,UserStatus 和 User。
UserStatus 有两个字段: 用户名,
用户状态有两个字段 用户名,已删除
这是我正在使用的查询
Update users
set deleted = '1'
where username = (select username
from tempDUDPIVOT
where status = 'inactive')
,但出现错误:
子查询返回超过 1 个值。当子查询跟在 =、!=、<、<=、>、>= 后面或子查询用作表达式时,这是不允许的。
我尝试了多种变体,但无法找出解决方案......我有一种感觉,它非常明显。
感谢您的帮助。
谢谢。
I have two tables, UserStatus and User.
UserStatus has two fields:
Username,Status
User has two fields
Username, Deleted
This is the query I'm using
Update users
set deleted = '1'
where username = (select username
from tempDUDPIVOT
where status = 'inactive')
but get the error:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
I've tried a number of variations but can't figure out the solution... I have a feeling its very obvious.
Your help is appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
UPDATE
语法需要标量子查询。使用EXISTS(子查询)
使谓词的参数更紧密地结合在一起(即使查询更易于阅读和维护,IMO),例如,如果您SQL 产品支持它(尽管请注意此处谓词的参数分开):
The
UPDATE
syntax requires a scalar subquery. UsingEXISTS (subquery)
bring the parameters of the predicate closer together (i.e. makes the query easier to read and maintain, IMO) e.g.You could also use Standard SQL
MERGE
if your SQL product supports it (though note the parameters of the predicate further apart here):IN
接受要返回的0..inf
值,=
接受 1 并且仅接受一个(不是 0 或 42)。IN
accepts0..inf
values to be returned, and=
accepts 1 and only one (not 0 or 42).