Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何生成下一个请求ID_Mysql_Perl_Sequence - Fatal编程技术网

Mysql 如何生成下一个请求ID

Mysql 如何生成下一个请求ID,mysql,perl,sequence,Mysql,Perl,Sequence,我有一个要求,其中请求_id将以 REQ0000001,REQ0000002….REQ0000010,REQ0000011….,REQ0000099 REQ0000100….就像前三个字符是REQ,后面是7个字符(数字是序列)。。。此请求id是mysql表中的主键 假设表中的最后一个条目是REQ0000009,下一个条目将是REQ0000010。。如何在perl中实现它 我采用以下方法: $sql_query = "select request_id from requests order by

我有一个要求,其中请求_id将以
REQ0000001,REQ0000002….REQ0000010,REQ0000011….,REQ0000099 REQ0000100….
就像前三个字符是REQ,后面是7个字符(数字是序列)。。。此请求id是mysql表中的主键

假设表中的最后一个条目是REQ0000009,下一个条目将是REQ0000010。。如何在perl中实现它

我采用以下方法:

$sql_query = "select request_id from requests order by request_id DESC LIMIT 1";
将此值存储在名为x的varibale中。然后

$x = reverse $x;  #Reverse the String
chop $x; # Chop the last Character (here R)

chop $x; # Chop the last Character (here E)

chop $x; # Chop the last Character (here Q)

$x = reverse $x; # Again Reverse
$x = $x  + 1; # Add 1

if ( length($x) eq 1) # if length ==1{

    $NextReq_id = 'REQ000000'.$x;

elsif ( length($x) eq 2)


    $NextReq_id = 'REQ00000'.$x;

elsif ( length($x) eq 3)


    $NextReq_id = 'REQ0000'.$x;

elsif ( length($x) eq 4)
{

$NextReq_id = 'REQ000'.$x;
} 

这里有更好的方法吗?

使用sprintf将字符串左填充0

sprintf("REQ%08d", $x)

您可以在perl中增加字符串:

if ($x lt 'REQ9999999') {
    $nextRequestId = $x;
    $nextRequestId++;
} else {
    // you ran out of request ids
}

(如果你不检查
req999999
你会在某个时候得到
RER0000000

这是实现我想要的目标的好方法。我想知道这种方法有什么后果吗?我已经描述了主要的后果,你必须注意你的增量不会滚到字符串的字母部分。除此之外,可能会有轻微的性能损失,但我相信与获取当前最大请求id所执行的查询的性能成本相比,这一损失可以忽略不计。您将如何处理
req999999
<代码>RER0000000或
REQ10000000
if ($x lt 'REQ9999999') {
    $nextRequestId = $x;
    $nextRequestId++;
} else {
    // you ran out of request ids
}