使用子查询更新多行

发布于 2024-11-06 12:35:53 字数 454 浏览 1 评论 0原文

我有两个表,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 技术交流群。

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

发布评论

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

评论(2

痴情 2024-11-13 12:35:53

UPDATE 语法需要标量子查询。使用EXISTS(子查询) 使谓词的参数更紧密地结合在一起(即使查询更易于阅读和维护,IMO),例如,

UPDATE users 
   SET deleted = '1' 
 WHERE EXISTS (
               SELECT *
                 FROM tempDUDPIVOT AS T1
                WHERE T1.status = 'inactive'
                      AND T1.username = users.username
              );

如果您SQL 产品支持它(尽管请注意此处谓词的参数分开):

MERGE INTO users 
   USING (
          SELECT username
            FROM tempDUDPIVOT
           WHERE status = 'inactive'
         ) AS T1 (username)
      ON T1.username = users.username
WHEN MATCHED THEN 
   UPDATE
      SET users.deleted = '1';

The UPDATE syntax requires a scalar subquery. Using EXISTS (subquery) bring the parameters of the predicate closer together (i.e. makes the query easier to read and maintain, IMO) e.g.

UPDATE users 
   SET deleted = '1' 
 WHERE EXISTS (
               SELECT *
                 FROM tempDUDPIVOT AS T1
                WHERE T1.status = 'inactive'
                      AND T1.username = users.username
              );

You could also use Standard SQL MERGE if your SQL product supports it (though note the parameters of the predicate further apart here):

MERGE INTO users 
   USING (
          SELECT username
            FROM tempDUDPIVOT
           WHERE status = 'inactive'
         ) AS T1 (username)
      ON T1.username = users.username
WHEN MATCHED THEN 
   UPDATE
      SET users.deleted = '1';
昔日梦未散 2024-11-13 12:35:53
Update users set deleted = '1'
where username IN (select username from tempDUDPIVOT where status = 'inactive')

IN 接受要返回的 0..inf 值,= 接受 1 并且仅接受一个(不是 0 或 42)。

Update users set deleted = '1'
where username IN (select username from tempDUDPIVOT where status = 'inactive')

IN accepts 0..inf values to be returned, and = accepts 1 and only one (not 0 or 42).

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