2017-03-06 64 views
0

嗨,大家好,先謝謝。 如果X is 50, Y is 0 or 1, Z is some integer將數值保存到一個數字範圍並根據新值更新以前保存的範圍

例子:

X is 40, Y is 1, Z is 2000 

我想在MySQL保存值

0 to 39 = -2000 and 40 to infinite = 2000 

以後如果X is 60, Y is 1 and Z is 3000

值應在MySQL

0 to 39 = -5000 
40 to 59 = -1000 
60 to infinite = 5000 
被保存0

我想在PHP中實現這一點。

+0

請您提供一下您的邏輯的更多描述。 –

+0

這對我沒有任何邏輯。你能解釋一下嗎? – Oliver

+0

經過上述所有變化後x = 55,Y = 1,z = 2000?後的結果是什麼? –

回答

0

我希望這有助於。我試圖涵蓋給定X值的所有可能性。

/*Your Table structure should be like this 
Here min and max are Bigint as we cannot use infinite so using the maximum value of bigint 

id min  max      value 
1 0  39      -2000 
2 40  9223372036854775807  2000 
*/ 
$x= from user; 
$y=from User; 
$z=from User; 
$negZ=$z*-1; // just to make negative value of $z 
$newid=0; // we will use these for update2 as well. 
//first we will check wether the give x range is exist or not IF exist then we dont need to add any more new row 

$sql=$conn->prepare("SELECT id FROM table FROM table WHERE min=:x"); 
$sql->execute(array(':x'=>$x)); 
while($col=$sql->fetch()) 
{ 
    $existRangeid=$col['id']; 
} 
if($existRangeid==0)//If these range doesn't exist then we need to add new row 
{ 
    //Now we will select the column which we gonna split. 
    $flag=false; 
    $sql=$conn->prepare("SELECT id, min,max,value FROM table WHERE min<:x AND max>:x"); 
    $sql->execute(array(':x'=>$x)); 
    while($row=$sql->fetch()) 
    { 
     $max=$row['max']; 
     $min=$row['min']; 
     $id=$row['id']; 
     $value=$row['value']; 
     $newVal=$value + $negZ; 
     $newMax=$x-1; 
     $flag=true; 
    } 
    if($flag==FALSE) // IF the given $x is same as max value of any row in DB then we need to follow //following logic. 
    // as an example if $x is 39 then it will run following code 
    { 
     $sql=$conn->prepare("SELECT id, min,max,value FROM table WHERE max=:x"); // we will find the row with max value is same as given x 
     $sql->execute(array(':x'=>$x)); 
     while($row=$sql->fetch()) 
     { 
      $max=$row['max']; 
      $min=$row['min']; 
      $id=$row['id']; 
      $value=$row['value']; 
      $newVal=$value + $negZ; 
      $newMax=$x-1; 

     } 
     //Now we will insert new range, it will have min and max's value same. 
     //e.g. Min = 39; Max= 39; 
     $insertsql=$conn->prepare('INSERT into table(min,max,value) VALUES(:min,:max,:value)'); 
     $insertsql->execute(array(
      ':min'=>$x, 
      ':max'=>$x, 
      ':value'=>$newVal 
      )); 
     $newid=$conn->lastInsertId(); // we will use this in Update 2 
     // Now we will update the range's max value. e.g. if x=39. first row with id 1 will now have 38 as a max value 
     $update1=$conn->prepare("UPDATE table SET max=:newmax WHERE id=:id"); 
     $update1->execute(array(':newmax'=>$newMax, ':id'=>$id)); 
    } 
    else 
    { 
     //Insert the new row with given range 
     $insertsql=$conn->prepare('INSERT into table(min,max,value) VALUES(:min,:max,:value)'); 
     $insertsql->execute(array(
      ':min'=>$min, 
      ':max'=>$newMax, 
      ':value'=>$newVal 
      )); 
     $newid=$conn->lastInsertId(); // we will use this in Update 2 

     //here we will update the minmun value of the row which we split. 
     $update1=$conn->prepare("UPDATE table SET min=:min WHERE id=:id"); 
     $update1->execute(array(':min'=>$x, ':id'=>$id)); 
    } 
} 
//if the give range exist into the system then condition id!=:newid will be id!=0 so it will update all the rows which satisfy max<:x condition. 
// IF the range doesnt exist then we wont update newly inserted row because it already contain correct value check insertsql 
$update2=$conn->prepare("UPDATE table SET value=(value+:negZ) WHERE max<:x AND id!=:newid"); 
$update2->execute(array(
    ':negZ'=>$negZ, 
    ':x'=>$x, 
    ':newid'=>$newid 
    )); 

// This will update all the ranges above given x by adding give z to the existing value 
$update3=$conn->prepare("UPDATE table SET value=(value+:z) WHERE min>=:x"); 
$update3->execute(array(
    ':z'=>$z, 
    ':x'=>$x 
)); 

讓我知道你是否還有關於這個邏輯的查詢。

+0

嗨,它給錯誤調用未定義的方法mysqli :: lastInsertId(), 和它不與空白表工作。 –

+0

@EkanshJain對於mysqli你必須使用函數調用。 mysqli_insert_id($ connection_variable); 總之,你需要得到你插入的最後一行的ID。 –