如何在SQL中找到范围差距 [英] How to find a gap in range in SQL

查看:85
本文介绍了如何在SQL中找到范围差距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题说明了如何在表中找到第一个未使用的数字,但是如何找到相同的数字,以便定义额外的约束。如何更改查询,以便获得大于100的第一个未使用的数字。

This question explains how to find the first "unused" number in a table, but how can I find the same so that I can define extra constraints. How do I alter the query so that I get the first unused number after that's greater than 100

例如如果我的表中有23、56、100、101、103,我应该得到102。

e.g. If I have 23, 56, 100, 101, 103 in my table i should get 102.

推荐答案

在mysql和postgresql

in mysql and postgresql

SELECT  id + 1
FROM    test mo
WHERE   NOT EXISTS
        (
        SELECT  NULL
        FROM    test mi 
        WHERE   mi.id = mo.id + 1
        ) and mo.id> 100
ORDER BY
        id
LIMIT 1

用于mysql的小提琴为PostgreSQL拨弄

在ms sql中

SELECT  TOP 1
        id + 1
FROM    test mo
WHERE   NOT EXISTS
        (
        SELECT  NULL
        FROM    test mi 
        WHERE   mi.id = mo.id + 1
        )
          and mo.id > 100
ORDER BY
        id

小提琴

这篇关于如何在SQL中找到范围差距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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