MySQL从表中获取丢失的ID [英] MySQL get missing IDs from table

查看:44
本文介绍了MySQL从表中获取丢失的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 MySQL 中有这个表,例如:

I have this table in MySQL, for example:

ID | Name
1  | Bob
4  | Adam
6  | Someguy

如果您注意到,没有 ID 号(2、3 和 5).

If you notice, there is no ID number (2, 3 and 5).

如何编写查询以便 MySQL 仅回答缺少的 ID,在本例中为:2,3,5"?

How can I write a query so that MySQL would answer the missing IDs only, in this case: "2,3,5" ?

推荐答案

SELECT a.id+1 AS start, MIN(b.id) - 1 AS end
    FROM testtable AS a, testtable AS b
    WHERE a.id < b.id
    GROUP BY a.id
    HAVING start < MIN(b.id)

希望这个链接也有帮助http://www.codediesel.com/mysql/sequence-gaps-in-mysql/

这篇关于MySQL从表中获取丢失的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆