在mysql表中查找最小未使用值 [英] Find minimum not used value in mysql table

查看:71
本文介绍了在mysql表中查找最小未使用值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的MySQL表:

I have a MySQL table like this:

+--------+-------+
| Server | Port  |
+--------+-------+
| 1      | 27001 |
| 2      | 27002 |
| 4      | 27004 |
+--------+-------+

您怎么看-这是27003端口-但它是在一段时间前删除的(例如).

How you can see - here was 27003 port - but it was deleted some time ago (just for example).

是否可以通过某种方式知道mysql表中未使用的最小值(例如,高于27000)? 还是没有办法,我需要用"used"或"not used"的所有端口和列创建一个单独的表?

Is there some way to know the minimum value that is not used (higher than 27000 for example) in mysql table? Or there is no way and I need to make an separate table with all ports and column with "used" or "not used" ?

情况说明:我正在创建游戏托管网站,并且可以对端口使用自动递增功能,但是一段时间后,我将有很多已删除/未使用的端口,并且我想在增加端口范围之前使用所有这些端口

Situation explanation: I am creating game hosting website, and I can use auto-increment for ports, but I will have a lot of deleted/unused ports after some time, and I want to use all of them before increasing port range.

推荐答案

在Google上快速搜索"

A quick search on Google for "first missing number from sequence mysql" gives this page of common MySQL queries.

它向您展示如何从序列中找到第一个缺少的数字 :

您有一个表tbl(id int),其值是(1,2,4,18,19,20,21),并且希望在其ID值序列中找到第一个缺失的数字:

SELECT t1.id+1 AS Missing 
FROM tbl AS t1 
LEFT JOIN tbl AS t2 ON t1.id+1 = t2.id 
WHERE t2.id IS NULL 
ORDER BY t1.id LIMIT 1; 

+---------+ 
| Missing | 
+---------+ 
|       3 | 
+---------+ 

这篇关于在mysql表中查找最小未使用值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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