SQL排除查询

发布于 2024-11-26 16:06:43 字数 267 浏览 0 评论 0原文

是否可以在单个 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 技术交流群。

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

发布评论

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

评论(6

唱一曲作罢 2024-12-03 16:06:43

假设电话号码是一个 NUMBER,您可以生成特定范围内所有电话号码的列表,

 SELECT level - 1 + 8001231000
   FROM dual
CONNECT BY level <= 8001239999-8001231000+1

然后您可以将该范围内所有电话号码的列表加入到实际的电话号码表中。像这样的东西

WITH all_numbers AS (
  SELECT level - 1 + 8001231000 phone_number
    FROM dual
 CONNECT BY level <= 8001239999-8001231000+1
)
SELECT *
  FROM all_numbers a
 WHERE NOT EXISTS(
    SELECT 1
      FROM phone_numbers p
     WHERE a.phone_number = p.phone_number)

Assuming that the phone number is a NUMBER, you can generate the list of all phone numbers in a particular range

 SELECT level - 1 + 8001231000
   FROM dual
CONNECT BY level <= 8001239999-8001231000+1

You can then join this list of all the phone numbers in the range to your actual table of phone numbers. Something like

WITH all_numbers AS (
  SELECT level - 1 + 8001231000 phone_number
    FROM dual
 CONNECT BY level <= 8001239999-8001231000+1
)
SELECT *
  FROM all_numbers a
 WHERE NOT EXISTS(
    SELECT 1
      FROM phone_numbers p
     WHERE a.phone_number = p.phone_number)
清浅ˋ旧时光 2024-12-03 16:06:43

如果您的电话号码是字符:

select * from mytable
where phone_number not between '8001231000' and '8001239999'

或者如果是数字:

select * from mytable
where phone_number not between 8001231000 and 8001239999

If your phone numbers are character:

select * from mytable
where phone_number not between '8001231000' and '8001239999'

or if they are numeric:

select * from mytable
where phone_number not between 8001231000 and 8001239999
滴情不沾 2024-12-03 16:06:43

我将加载一个临时表,其中包含您要检查的范围内的所有 10000 个电话号码,并执行排除连接:

SELECT a.phone_number
FROM phone_numbers_i_want_to_check AS a
LEFT OUTER JOIN phone_numbers AS b
  ON a.phone_number = b.phone_number
WHERE b.phone_number IS NULL;

I would load a temporary table with all 10000 phone numbers in the range you want to check, and do an exclusion join:

SELECT a.phone_number
FROM phone_numbers_i_want_to_check AS a
LEFT OUTER JOIN phone_numbers AS b
  ON a.phone_number = b.phone_number
WHERE b.phone_number IS NULL;
赢得她心 2024-12-03 16:06:43

您正在寻找带有与这些电话号码匹配的子查询的“NOT IN”运算符。

You are looking for the "NOT IN" Operator with a subquery matching those telephone numbers.

网白 2024-12-03 16:06:43

我想不出一种方法可以通过单个选择来完成此操作,但您可以通过单个事务来完成此操作。具体来说:

  • 使用您范围内的值创建一个临时表(应该可以通过单个创建表和一个插入来实现)
  • 从临时表中删除主表中确实存在的值
  • 从临时表中选择剩余的值

<强>编辑:比尔·卡尔文的答案更好。与临时表的概念相同,但是只需一次选择即可提取不存在的值。

I can't think of a way to do it with a single select, but you can do it with a single transaction. Specifically:

  • Create a temp table with the values in your range (should be possible with a single create table and one insert)
  • DELETE the values from your temp table that do exist in your main table
  • SELECT the values from the temp table that are left

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.

不弃不离 2024-12-03 16:06:43

像这样的事情:

SELECT numbers_array.num
  FROM (SELECT 8001231000 + LEVEL num 
          FROM dual 
       CONNECT BY LEVEL <= (8001239999 - 8001231000)
       ) numbers_array
 WHERE numbers_array.num NOT IN (SELECT number_you_have FROM your_table_of_numbers)

创建所有可能数字的伪列表并从中排除现有数字。

Something like this:

SELECT numbers_array.num
  FROM (SELECT 8001231000 + LEVEL num 
          FROM dual 
       CONNECT BY LEVEL <= (8001239999 - 8001231000)
       ) numbers_array
 WHERE numbers_array.num NOT IN (SELECT number_you_have FROM your_table_of_numbers)

Create a pseudo-list of all possible numbers and exclude existing numbers from it.

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