问题:在存在值之间为服务器获取第一个空闲端口。如果没有空位,则取最高的+1。
额外:如果 server_deleted=1 那么我们可以使用空闲端口。当价值观之间存在差距时,我们也应该选择自由港。我们应该在 where 子句中包含 server_type。最小值是第一个端口,例如 9000。
现在我的查询看起来:
SELECT server_port + 1
FROM pro_servers s
WHERE s.server_port <> 0
AND s.server_type = 'ts3'
AND s.server_deleted = 0
AND NOT EXISTS
( SELECT s1.server_port
FROM pro_servers s1
WHERE s1.server_port <> 0
AND s1.server_type = 'ts3'
AND s1.server_port = s.server_port + 1
AND s1.server_deleted = 0
)
ORDER BY server_port LIMIT 1
我认为有更好的方法来做到这一点。这个查询执行很慢。
例如first=minimum=lowest = 9000,接下来是9002,9003。我们需要得到9001。如果我们添加9001,则得到9004。第一个值9000存在于表中。
样本数据
+-----------+-------------+----------------+-------------+
| server_id | server_port | server_deleted | server_type |
+-----------+-------------+----------------+-------------+
| 151 | 9500 | 1 | teamspeak3 |
| 8459 | 9500 | 0 | teamspeak3 |
| 183 | 9501 | 1 | teamspeak3 |
| 264 | 9502 | 1 | teamspeak3 |
| 4155 | 9502 | 1 | teamspeak3 |
| 2707 | 9503 | 1 | teamspeak3 |
| 4160 | 9503 | 1 | teamspeak3 |
| 154 | 9504 | 1 | teamspeak3 |
| 4163 | 9504 | 1 | teamspeak3 |
| 285 | 9506 | 1 | teamspeak3 |
| 4167 | 9506 | 1 | teamspeak3 |
| 8454 | 9506 | 0 | teamspeak3 |
| 241 | 9507 | 1 | teamspeak3 |
| 4169 | 9507 | 1 | teamspeak3 |
| 188 | 9509 | 1 | teamspeak3 |
| 4177 | 9509 | 1 | teamspeak3 |
+-----------+-------------+----------------+-------------+
查询结果:9501。当我们使用此端口时,接下来:9502、9503、9504、9505、9507、9508、9509、9510 等。