SQL - Find missing int values in mostly ordered sequential series

前端 未结 6 635
有刺的猬
有刺的猬 2020-12-06 08:59

I manage a message based system in which a sequence of unique integer ids will be entirely represented at the end of the day, though they will not necessarily arrive in orde

6条回答
  •  囚心锁ツ
    2020-12-06 09:09

    I applied it in mysql, it worked ..

    mysql> select * from sequence;
    +--------+
    | number |
    +--------+
    |      1 |
    |      2 |
    |      4 |
    |      6 |
    |      7 |
    |      8 |
    +--------+
    6 rows in set (0.00 sec)
    
    mysql> SELECT t1.number - 1 FROM sequence AS t1 LEFT OUTER JOIN sequence AS t2 O
    N t1.number = t2.number +1 WHERE t2.number IS NULL;
    +---------------+
    | t1.number - 1 |
    +---------------+
    |             0 |
    |             3 |
    |             5 |
    +---------------+
    3 rows in set (0.00 sec)
    

提交回复
热议问题