大家好,先谢谢。
如果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
我想在PHP中实现这一点。
答案 0 :(得分: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
));
如果您对此逻辑有任何疑问,请与我们联系。