两个表中的行数差异
如何计算两个不同表中行数的差异?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
减号运算符用于删除第一个结果集中也包含在第二个结果集中的所有记录。此处使用 -(破折号)运算符。
minus operator is used to removed all records of first result set that are also contained in second. Use - (dash) operator here.
MINUS是一个SQL集合操作,这里不需要,只是使用
-
。MINUS is a SQL set operation that is not needed here, just use
-
.以下内容确实很快(对于包含数亿条记录的表,不到 1 秒),前提是您的统计信息是最新的(强烈建议这样做)。
适用示例代码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).
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
在 Sqlite 中,这可以工作:
In Sqlite, this would works: