作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个需求,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;
}
这里有更好的方法吗?
最佳答案
你可以在 perl 中递增字符串:
if ($x lt 'REQ9999999') {
$nextRequestId = $x;
$nextRequestId++;
} else {
// you ran out of request ids
}
(如果您不检查 REQ9999999
,您最终会得到 RER0000000
)
关于mysql - 如何生成下一个请求 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10773442/
我是一名优秀的程序员,十分优秀!