【发布时间】:2025-12-06 18:45:02
【问题描述】:
我有一个要求,其中 request_id 的形式为
REQ0000001,REQ0000002....REQ0000010, REQ0000011...., REQ0000099 REQ0000100..... 就像前三个字符是 REQ 后跟 7 个字符(数字是序列)...这个 request_id 是 mysql 表中的主键。
假设表中的最后一个条目是 REQ0000009,下一个条目将是 REQ0000010.. 在 perl 中如何做??
我正在使用以下方式:
$sql_query = "select request_id from requests order by request_id DESC LIMIT 1";
将此值存储在名为 x 的变量中。那么
$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;
}
这里有更好的方法吗?
【问题讨论】:
-
你会如何处理
REQ9999999?RER0000000或REQ10000000?