两个表中的行数差异

发布于 2024-10-12 11:55:58 字数 372 浏览 8 评论 0原文

如何计算两个不同表中行数的差异?

SQL> select count(*) from dual44;

  COUNT(*)
----------
         3

SQL> select count(*) from dual;

  COUNT(*)
----------
         1

SQL> (select count(*) from dual44)
  2  minus
  3  (select count(*) from dual)
  4  ;

  COUNT(*)
----------
         3

SQL> 

我需要 2 作为结果。这两个表不一定具有相同的架构。

How can I take a difference in the counts of the number of rows in two different tables?

SQL> select count(*) from dual44;

  COUNT(*)
----------
         3

SQL> select count(*) from dual;

  COUNT(*)
----------
         1

SQL> (select count(*) from dual44)
  2  minus
  3  (select count(*) from dual)
  4  ;

  COUNT(*)
----------
         3

SQL> 

I need 2 as the result. The two tables might not necessarily have the same scehma.

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

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

发布评论

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

评论(5

蓝海似她心 2024-10-19 11:55:58

减号运算符用于删除第一个结果集中也包含在第二个结果集中的所有记录。此处使用 -(破折号)运算符。

select ((select count(*) from dual44) - (select count(*) from dual)) from dual

minus operator is used to removed all records of first result set that are also contained in second. Use - (dash) operator here.

select ((select count(*) from dual44) - (select count(*) from dual)) from dual
浪荡不羁 2024-10-19 11:55:58
select count(*) - (select count(*) from dual)
from dual44

MINUS是一个SQL集合操作,这里不需要,只是使用-

select count(*) - (select count(*) from dual)
from dual44

MINUS is a SQL set operation that is not needed here, just use -.

梦中楼上月下 2024-10-19 11:55:58

以下内容确实很快(对于包含数亿条记录的表,不到 1 秒),前提是您的统计信息是最新的(强烈建议这样做)。

select 
(
(select u.NUM_ROWS from user_tables u where u.TABLE_NAME='JOBS')
 - 
(select u.NUM_ROWS from user_tables u where u.TABLE_NAME='COUNTRIES')
 ) DIFF
from dual


     DIFF
----------
        -6

适用示例代码ORACLE 的 HR 模式。您可以更改表名称。

这两个表可能不一定具有相同的架构。

编辑 Jeffrey Kemp 的评论:

如果表位于不同的架构上,则必须使用 dba_tables ,如果你有特权的话。

只需将架构名称添加到表名称即可。即HR.JOBS

The following is really fast ( less than 1 second for tables of hundreds of millions records) provided that your statistics are up to date (which is highly recommended).

select 
(
(select u.NUM_ROWS from user_tables u where u.TABLE_NAME='JOBS')
 - 
(select u.NUM_ROWS from user_tables u where u.TABLE_NAME='COUNTRIES')
 ) DIFF
from dual


     DIFF
----------
        -6

Sample code applies to HR schema of ORACLE. You can change the table names.

The two tables might not necessarily have the same schema.

Edit on Jeffrey Kemp's comment:

If tables are on different schemas, you have to use dba_tables, if you have the privileges.

Just add the schema name to the table name. i.e. HR.JOBS

双手揣兜 2024-10-19 11:55:58
SELECT (a.count-b.count)
 FROM
   (SELECT COUNT(*) count FROM dual44) a,
   (SELECT COUNT(*) count FROM dual) b;
SELECT (a.count-b.count)
 FROM
   (SELECT COUNT(*) count FROM dual44) a,
   (SELECT COUNT(*) count FROM dual) b;
冷默言语 2024-10-19 11:55:58

Sqlite 中,这可以工作:

Select (Select count(col1) from Table1) - (Select count(col2) from Table2)) 

In Sqlite, this would works:

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