简体   繁体   English

从mysql ID生成获取下一个唯一编号

[英]generate get next unique number from mysql ID

I need to generate a unique extension for each user (this is separate from the ID but works much the same) the number should be 100 or greater and can be overwritten/custom set. 我需要为每个用户生成一个唯一的扩展名(该扩展名与ID分开,但工作原理基本相同),该数字应为100或更大,并且可以覆盖/自定义设置。 Right now I am taking the next id and if it's less than 100 add 100. 现在,我使用下一个ID,如果它小于100,则添加100。

So if the next id is 5 the number would be 105 but it the next id is 105 the number would just be 105. The problem is that because i'm letting the user pick their own extension if a user choose 105 before I want to make it automatically jump to the next number in this case 106. Now if there is 105, 106, and 108 I would want to jump to 107 then jump to 109. Here is the code I'm using to generate the numbers. 因此,如果下一个ID为5,则数字将为105,但下一个ID为105,数字将仅为105。问题是,因为如果用户在我想选择105之前选择了自己的分机号,我就让用户自行选择使其自动跳到下一个数字(在这种情况下为106)。现在,如果有105、106和108,我想跳到107,然后跳到109。这是我用来生成数字的代码。 I think the problem is with my while loop. 我认为问题出在我的while循环上。 I'm not sure how to make it keep on checking for unique numbers. 我不确定如何继续检查唯一数字。

Here is the code, I'm sure I'm overcomplicated things A LOT. 这是代码,我确定我把事情复杂化了很多。

$result = mysql_query("SELECT MAX(id) 
                         FROM USERS");
$row = mysql_fetch_row($result);
$sql_extention = intval($row[0]);

//make sure it's at least 100
$extension = ($sql_extension < 100) ? $sql_extension+100 :  $sql_extension;

//check to see if the extention is in use
$qry = "SELECT `extention` 
          FROM users 
         WHERE extention = '$extention'";
$result2 = mysql_query($qry);

//if the extention is in use then find the next available one (this isn't currently working)
if($result2) {
  //get all results greater or equal to our extention
  $qry3 = "SELECT `id`,`extention` 
             FROM admin_users 
            WHERE extention >= '$extention'";

  $result3 = mysql_query($qry3);
  //this loop needs to be rewritten somehow to get the next number by checking if the next number exist if not return that as the extention
  $new_extention = $extention+1;

  while($extention_data = mysql_fetch_array($result3)) {
    if($new_extention != $extention_data['extention']+1) {
      $extention = $new_extention;
    }

  $new_extention++;
}

I came up with this, haven't tested it thoroughly but i think it should return the next available value correctly 我想出了这个,还没有彻底测试,但是我认为它应该正确返回下一个可用值

SELECT (a.extention + 1) as avail 
FROM admin_users a LEFT JOIN admin_users b on ( (a.extention + 1) = b.extention )
WHERE a.extention >= 105 and b.extention is null
ORDER BY avail ASC 
LIMIT 1

So if this works as expected you wont need the last few lines of code at all. 因此,如果这按预期工作,则根本不需要最后几行代码。

Edit: Revised the query because i realized i was approaching it from the wrong side. 编辑:修改查询,因为我意识到我正在从错误的方面着手。

Shoddy attempt at PHP/pseudocode example as per my comment: 根据我的评论,在PHP /伪代码示例中进行的卑鄙尝试:

//nicer way to get the id of the user you just inserted!
$id = mysql_insert_id();

$sql = "SELECT `extension` FROM users ORDER BY `extension` ASC";
$res = mysql_query($sql);

$i=0;
while($n = mysql_fetch_array($res)){
  if($i==0){
    $i=$n['extension'];
  }
  if($i==$n['extension']){
    $i++;
  } else {
    break;
  }
}

//No existing users, start at 100
if($i==0){
  $i=100;
}

Then use $i as your extension. 然后使用$ i作为扩展名。

Ok, so you need the next available extention higher then a given number, that is not already in the database. 好的,因此您需要一个比给定数字高的下一个可用扩展,该数字尚未存在于数据库中。 So, you ideally want an array from the database that has all available extentions higher then a given key sorted ascending. 因此,理想情况下,您希望数据库中的数组具有比给定键升序排列的所有可用范围更高的扩展。 Then you loop from the given number increasing by one, until it does not match. 然后,您从给定的数字开始加1,直到不匹配为止。 You don't mention a max number of extention. 您没有提及最大扩展数。 I would do it like this: 我会这样做:

<?php
$rs = mysql_unbuffered_query("SELECT extention, MAX(extention) FROM admin_users WHERE extention > '$extention' ORDER BY extention ASC");
$unavailable = array();
$max = 0;
while( ($row = mysql_fetch_row($rs)) )
{
    $unavailable[] = $row[0];
    if( !$max ) $max = $row[1];
}
mysql_free_result($rs);
// Optimization for the quite common case where no more extentions are available
if( count($unavailable) > 0 )
{
    while($extention <= $max+1)
    {
        if( !in_array($extention, $unavailable) )
            break;
        $extention++;
    }
}
else
    $extention = $max+1;
// Worst case: $extention now is max + 1 and we looped through almost everything.
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM