在PLSQL Server中使用子查询更新查询

发布于 2025-02-12 03:45:46 字数 682 浏览 0 评论 0原文

我比较了Table1数据Table2数据,以及表1中不存在的过滤数据。我想让两个表行计数相等。为此,我需要在表2中设置is_Active = 0,其中表1中不存在数据。

示例::

UPDATE table2 
SET table2.is_active = 0 
WHERE (SELECT table2.user_name 
       FROM table2 
       WHERE table2.is_Active = 1 
         AND table2.user_name NOT IN (SELECT table1.user_name 
                                      FROM table1 
                                      WHERE table1.is_Active = 1));
==table1==      ==table2==
'A', 'B', 'C'   'A', 'B', 'C'
'A', 'E', 'C'   'M', 'N', 'O'
'A', 'E', 'D'   'A', 'E', 'D'
                'A', 'E', 'D'

在上面的示例中,表1和Table2包含相似的数据,除了'm','n','o',我希望将此行设置为is_Attive = 0状态。

I compared table1 data table2 data, and filtered data which was not existed in table1. I want to make both tables rows count equal. For that, I need to set is_active = 0 in table2, where the data was not existed in table1.

Example::

UPDATE table2 
SET table2.is_active = 0 
WHERE (SELECT table2.user_name 
       FROM table2 
       WHERE table2.is_Active = 1 
         AND table2.user_name NOT IN (SELECT table1.user_name 
                                      FROM table1 
                                      WHERE table1.is_Active = 1));
==table1==      ==table2==
'A', 'B', 'C'   'A', 'B', 'C'
'A', 'E', 'C'   'M', 'N', 'O'
'A', 'E', 'D'   'A', 'E', 'D'
                'A', 'E', 'D'

In above example, table 1 and table2 contains similar data except 'M', 'N', 'O', I want set this row into is_active = 0 state.

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

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

发布评论

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

评论(1

相权↑美人 2025-02-19 03:45:46

对我来说,不存在更新似乎是一种选择。

示例数据:

SQL> select * from table1;

COL1  COL2  COL3   IS_ACTIVE
----- ----- ----- ----------
a     b     c              1
a     e     c              1
a     e     d              1

SQL> select * from table2;

COL1  COL2  COL3   IS_ACTIVE
----- ----- ----- ----------
a     b     c              1
a     e     c              1
a     e     d              1
m     n     o              1   --> IS_ACTIVE should be set to 0

更新:

SQL> update table2 b set
  2    b.is_active = 0
  3    where not exists (select null from table1 a
  4                      where a.col1 = b.col1
  5                        and a.col2 = b.col2
  6                        and a.col3 = b.col3
  7                     );

1 row updated.

结果:

SQL> select * from table2;

COL1  COL2  COL3   IS_ACTIVE
----- ----- ----- ----------
a     b     c              1
a     e     c              1
a     e     d              1
m     n     o              0   --> IS_ACTIVE is set to 0

SQL>

To me, update with not exists seems to be one option.

Sample data:

SQL> select * from table1;

COL1  COL2  COL3   IS_ACTIVE
----- ----- ----- ----------
a     b     c              1
a     e     c              1
a     e     d              1

SQL> select * from table2;

COL1  COL2  COL3   IS_ACTIVE
----- ----- ----- ----------
a     b     c              1
a     e     c              1
a     e     d              1
m     n     o              1   --> IS_ACTIVE should be set to 0

Update:

SQL> update table2 b set
  2    b.is_active = 0
  3    where not exists (select null from table1 a
  4                      where a.col1 = b.col1
  5                        and a.col2 = b.col2
  6                        and a.col3 = b.col3
  7                     );

1 row updated.

Result:

SQL> select * from table2;

COL1  COL2  COL3   IS_ACTIVE
----- ----- ----- ----------
a     b     c              1
a     e     c              1
a     e     d              1
m     n     o              0   --> IS_ACTIVE is set to 0

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