自动递增作业编号,在每个月的InnoDB锁定表开始时重置

时间:2013-11-25 12:11:37

标签: php mysql

我在2天前提出了类似的问题但是虽然我接受了解决方案,但它涉及使用MyISAM引擎。经过一些研究我发现我really shouldn't use MyISAM, for many reasons.所以我决定在InnoDB引擎中实现以下结果我被告知我需要使用我不熟悉的表锁定。

我想要达到的结果是唯一的工作号,例如1311/5。显示年份的前两位数13,接下来的两位11 {1}}斜杠后的月份和数字我希望它是一个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变量中,我不能在最后一个数字之前将.'/'.斜线这样的斜线插入,任何人都知道为什么?我尝试使用双引号并且它也没有工作。好像现在我已经尝试将此页面上传到我的实际网站了,虽然它在本地工作但它并不在线:((

2 个答案:

答案 0 :(得分:0)

试试这个..

   $ref = $zan4.$zan5.'/'.$realcount;

答案 1 :(得分:0)

仅当 concurrency不是问题,那么在类似的问题中您可以使用我的解决方案:

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")) {

            }

    }
}