SQL排除查询
是否可以在单个 SQL 语句中执行以下操作:
在提示中使用电话号码的子集,例如 8001231000-8001239999
。然后查询我的数据库中有电话号码,并返回原始子集中哪些电话号码不在我的数据库中?我的数据库是Oracle 10g。
基本上,我不想返回 8001231000-8001239999
之间的电话号码,而是想知道 8001231000-8001239999
之间的哪些电话号码不在我的数据库中。
Is it possible in a single SQL statement to do the following:
Use a subset of telephone numbers in a prompt, for example 8001231000-8001239999
. Then query my database that has phone numbers in it, and return which phone numbers in the original subset are NOT in my database? My db is Oracle 10g.
Basically instead of bringing back which phone numbers ARE between 8001231000-8001239999
, I want to know which phone numbers between 8001231000-8001239999
are NOT in my database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
假设电话号码是一个 NUMBER,您可以生成特定范围内所有电话号码的列表,
然后您可以将该范围内所有电话号码的列表加入到实际的电话号码表中。像这样的东西
Assuming that the phone number is a NUMBER, you can generate the list of all phone numbers in a particular range
You can then join this list of all the phone numbers in the range to your actual table of phone numbers. Something like
如果您的电话号码是字符:
或者如果是数字:
If your phone numbers are character:
or if they are numeric:
我将加载一个临时表,其中包含您要检查的范围内的所有 10000 个电话号码,并执行排除连接:
I would load a temporary table with all 10000 phone numbers in the range you want to check, and do an exclusion join:
您正在寻找带有与这些电话号码匹配的子查询的“NOT IN”运算符。
You are looking for the "NOT IN" Operator with a subquery matching those telephone numbers.
我想不出一种方法可以通过单个选择来完成此操作,但您可以通过单个事务来完成此操作。具体来说:
<强>编辑:比尔·卡尔文的答案更好。与临时表的概念相同,但是只需一次选择即可提取不存在的值。
I can't think of a way to do it with a single select, but you can do it with a single transaction. Specifically:
Edit: Bill Karwin's answer is better. Same concept with the temp table, but then a single select to pull out the values that don't exist.
像这样的事情:
创建所有可能数字的伪列表并从中排除现有数字。
Something like this:
Create a pseudo-list of all possible numbers and exclude existing numbers from it.