SQL:选择尚不存在的最低值 [英] SQL: Select lowest value that doesn't already exist
本文介绍了SQL:选择尚不存在的最低值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 TableA 中,我有一个 int 列.
In TableA I have an int column.
是否可以仅使用 select 语句来选择列中不存在且大于 0 的最小值?
Is it possible using only a select statement to select the minimum value in the column that DOES NOT EXIST and is greater then 0?
例如,如果 col 的值为 1,2,9,则 select 语句将返回 3.如果 col 有 9,10,11,它将返回 1.
For example, if the col has the values 1,2,9 the select statement will return 3. If the col has 9,10,11 it will return 1.
我可以使用临时表或使用循环来实现这一点,但我想知道是否可以仅使用 select 语句来实现?
I can achieve this using a temp table or using a loop, but I'm wondering if I can do it using just a select statement?
推荐答案
select
min(nt.id)
from numbertable nt
left outer join originaldata od
on nt.id=od.id
where od.id is null
有一个从 1 到最大值(或更高)的数字表
have a number table that goes from 1 to your max value (or higher)
这篇关于SQL:选择尚不存在的最低值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文