2013-11-25 47 views
0

我在2天前提出了類似的問題,但儘管我接受了一個解決方案,但它涉及使用MyISAM引擎。經過一番研究後,我發現我really shouldn't use MyISAM, for many reasons. So我決定在InnoDB引擎中實現以下結果,在那裏我被告知我需要使用表鎖,而我並不那麼熟悉它。自動增量工作號在每個月初重置InnoDB鎖定表

我想達到的效果是唯一一個工號是這樣的:1311/5。隨着兩個第一位數13顯示年份,下兩11月份和斜線後的數字我想它是一個auto_increment數字,每個月都會重置,並將作爲工作臺。

更新:,因爲我有沒有腦震盪關注我workded我們在跟隨着代碼上面的問題:

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變量我不能concatonate .'/'.斜線這樣的最後一個數字之前任何人有任何想法爲什麼?我試着用雙引號和它不工作either.Ans好像這wasnt夠了,我試圖上傳此頁到我的實際網站,而這是在本地工作它不在線:((

回答

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

      } 

    } 
} 
0

試試這個..

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

這就是不工作:) –