![](/img/trans.png)
[英]Unique auto increment reference number that resets at the 1st of each month
[英]Auto increment job number that resets at the start of each month InnoDB locking tables
我两天前也提出了类似的问题,但尽管我接受了一个解决方案,但它涉及使用MyISAM引擎。经过一些研究后,我发现我真的不应该使用MyISAM,原因有很多。 因此,我决定在InnoDB引擎中实现以下结果,在该引擎中,我被告知我需要使用我不太熟悉的表锁定。
我要实现的结果是一个唯一的工作编号 ,如下所示: 1311/5
前两个数字13
表示年份,接下来的两个11
表示月份和斜杠后的数字,我希望它是一个auto_increment该号码将每月重置,并用作工作计数器。
更新:由于我没有脑震荡问题,因此我针对以下问题设计了以下代码:
if($mar = $db->query("SELECT max(id) FROM jobs")) {
if($mar2 = $mar->fetch_row()) {
$mar3 = $mar2[0]; //----> max id from jobs table
$mar4 = $mar3 - 1; //--> the 2nd biggest id which has the last inserted ref
}
}
if($vak = $db->query("SELECT * FROM jobs where id = $mar4")) {
if($vak2 = $vak->fetch_object()) {
$vak3 = $vak2->case_reference;
$vak3_len = strlen($vak3);
$vak4 = substr($vak3, 4); //----> the last number of the last job num
$vak5 = $vak2->created;
$vak7 = substr($vak5, 8, 2); //----> the date(d) of the one which has a job num
}
}
if($zan = $db->query("SELECT * FROM jobs where id = $mar3")) {
if($zan2 = $zan->fetch_object()) {
$zan3 = $zan2->created;
$zan4 = substr($zan3, 2, 2); //----> the date(y) of the one without job num
$zan5 = substr($zan3, 5, 2); //----> the date(m) of the one without job num
$zan7 = substr($zan3, 8, 2); //----> the date(d) of the one without job num
}
}
$realcount = $vak4+1;
$ace = 1;
if($zan7 >= $vak7) {
$ref = $zan4.$zan5.$realcount;
} else { $ref = $zan4.$zan5.$ace; } //----> $ref = the job num that gets inputed in new inserts
if($sqlref = $db->query("UPDATE `prinseapals`.`jobs` SET `case_reference` = $ref WHERE `jobs`.`id` = $mar3")) {
}
所需的表如下所示:
customer vessel created ref_num
nomikos lucky luke 2013-09-04 1309/25
allseas letto 2013-09-18 1309/26
medcare marina 2013-10-01 1310/1
golden kamari 2013-10-14 1310/2
marine winner 2013-11-01 1311/1
欢迎所有帮助,并让您知道我对PHP-MySQL真的很陌生。 提前致谢。 但是现在的问题是,在最后的$ ref变量中,我无法合并.'/'.
在最后一个数字之前这样的斜线,任何人都不知道为什么? 我尝试使用双引号并且也不起作用。这似乎还不够,现在我尝试将此页面上传到我的实际站点,并且在本地工作时它不在线:((
尝试这个..
$ref = $zan4.$zan5.'/'.$realcount;
仅在 并发不是问题的情况下,才可以在类似问题中使用我的解决方案:
if($mar = $db->query("SELECT max(id) FROM jobs")) { //----> $db = your connection and jobs = the table
if($mar2 = $mar->fetch_row()) {
$mar3 = $mar2[0]; //----> max id from jobs
$mar4 = $mar3 - 1; //----> the 2nd max id thich has on it assigned the last ref number
}
}
if($vak = $db->query("SELECT * FROM jobs where id = $mar4")) {
if($vak2 = $vak->fetch_object()) {
$vak3 = $vak2->case_reference;
$vak3_len = strlen($vak3);
$vak4 = substr($vak3, 5); //----> the last ref number
$vak5 = $vak2->created;
$vak7 = substr($vak5, 8, 2); //----> date(d) of the row which HAS a ref num
}
}
if($zan = $db->query("SELECT * FROM jobs where id = $mar3")) {
if($zan2 = $zan->fetch_object()) {
$zan3 = $zan2->created;
$zan4 = substr($zan3, 2, 2); //----> date(y) of the row which HAS NOT a ref num
$zan5 = substr($zan3, 5, 2); //----> date(m) of the row which HAS NOT a ref num
$zan7 = substr($zan3, 8, 2); //----> date(d) of the row which HAS NOT a ref num
$realcount = $vak4+1;
$ace = 1;
if($zan7 >= $vak7) {
$ref = $zan4.$zan5."/".$realcount;
} else { $ref = $zan4.$zan5."/".$ace; } //----> $ref = the final number you created to use
if($db->query("UPDATE jobs SET case_reference = '$ref' WHERE id = $mar3")) {
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.