我希望這有助於。我試圖涵蓋給定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
));
讓我知道你是否還有關於這個邏輯的查詢。
請您提供一下您的邏輯的更多描述。 –
這對我沒有任何邏輯。你能解釋一下嗎? – Oliver
經過上述所有變化後x = 55,Y = 1,z = 2000?後的結果是什麼? –