在MySQL表中找到最小的未使用值?
您可以使用LEFTJOIN查找MySQL表中的最小未使用值。让我们先创建一个表
create table FindValue -> ( -> SequenceNumber int -> );
使用insert命令在表中插入一些记录。查询如下-
insert into FindValue values(109); insert into FindValue values(110); insert into FindValue values(111); insert into FindValue values(113); insert into FindValue values(114);
使用select语句显示表中的所有记录。查询如下-
select *from FindValue;
以下是输出
+----------------+ | SequenceNumber | +----------------+ | 109 | | 110 | | 111 | | 113 | | 114 | +----------------+ 5 rows in set (0.00 sec)
这是在MySQL表中查找最小未使用值的查询
select tbl1 .SequenceNumber+1 AS ValueNotUsedInSequenceNumber -> from FindValue AS tbl1 -> left join FindValue AS tbl2 ON tbl1.SequenceNumber+1 = tbl2.SequenceNumber -> WHERE tbl2.SequenceNumber IS NULL -> ORDER BY tbl1.SequenceNumber LIMIT 1;
以下是输出
+------------------------------+ | ValueNotUsedInSequenceNumber | +------------------------------+ | 112 | +------------------------------+ 1 row in set (0.00 sec)
热门推荐
10 小升初送给儿子祝福语简短
11 冬至祝福语简短情话高级
12 烟酒店开业祝福语简短
13 大哥添孙子简短祝福语
14 滑稽搞笑结婚祝福语简短
15 CAD考试祝福语毕业简短
16 部队牛年祝福语简短的
17 友情祝福语简短小故事
18 男孩中考蛋糕祝福语简短