如何使用 SQL 查找序列中缺失的元素?

发布于 2024-10-20 11:03:30 字数 222 浏览 2 评论 0原文

我有一个表 bill,其列名称为 bill_idbill_id 值范围是从 1 到 40。我有类似

bill_id
-----------
1  
3  
6  
8  
2  
21  
34  
35  
26  
40

如何找到缺失元素(4、5、7、9、10 等)?

I have a table bill with column name bill_id. bill_id value range is from 1 to 40. And I have rows like

bill_id
-----------
1  
3  
6  
8  
2  
21  
34  
35  
26  
40

How can I find the missing elements (4, 5, 7, 9, 10, etc.)?

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

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

发布评论

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

评论(1

以为你会在 2024-10-27 11:03:30
SQL> create table bill (bill_id)
  2  as
  3  select 1 from dual union all
  4  select 3 from dual union all
  5  select 6 from dual union all
  6  select 8 from dual union all
  7  select 2 from dual union all
  8  select 21 from dual union all
  9  select 34 from dual union all
 10  select 35 from dual union all
 11  select 26 from dual union all
 12  select 40 from dual
 13  /

Table created.

SQL> with all_possible_bill_ids as
  2  ( select level bill_id
  3      from dual
  4   connect by level <= 40
  5  )
  6  select bill_id
  7    from all_possible_bill_ids
  8   minus
  9  select bill_id
 10    from bill
 11  /

   BILL_ID
----------
         4
         5
         7
         9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        22
        23
        24
        25
        27
        28
        29
        30
        31
        32
        33
        36
        37
        38
        39

30 rows selected.

问候,
抢。

SQL> create table bill (bill_id)
  2  as
  3  select 1 from dual union all
  4  select 3 from dual union all
  5  select 6 from dual union all
  6  select 8 from dual union all
  7  select 2 from dual union all
  8  select 21 from dual union all
  9  select 34 from dual union all
 10  select 35 from dual union all
 11  select 26 from dual union all
 12  select 40 from dual
 13  /

Table created.

SQL> with all_possible_bill_ids as
  2  ( select level bill_id
  3      from dual
  4   connect by level <= 40
  5  )
  6  select bill_id
  7    from all_possible_bill_ids
  8   minus
  9  select bill_id
 10    from bill
 11  /

   BILL_ID
----------
         4
         5
         7
         9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        22
        23
        24
        25
        27
        28
        29
        30
        31
        32
        33
        36
        37
        38
        39

30 rows selected.

Regards,
Rob.

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