逻辑要求,结构混淆查询

时间:2016-09-10 04:30:10

标签: php mysql

我的查询与数据库数据进行比较,如果数据库内容等于或大于搜索条件($ musage),则选择数据到结果中。

* - 1是值“无限制”

问题是当数据库数据为-1时,“实际上它小于每个正整数”,但在我的数据库中它比每个数字都大。执行select查询以将数据库的内容导入到结果中,如何在数据库内容等于-1时控制情况?

这是我的代码:

$device = $_POST['device'];
$provider1 = $_POST['provider1'];
$plantype = $_POST['plantype'];
$dusage = $_POST['dusage'];
$cusage = $_POST['cusage'];
$musage = $_POST['musage'];
//default query
$MSGQuery = " plan.`MSG` >= $musage ";
//when search criteria is unlimited
if( $musage == 'Unlimited' ){
    $MSGQuery = " plan.`MSG` = -1 ";
}//when search criteria is 0
else if ( $musage == 0 ){
    $MSGQuery = " plan.`MSG` >= -1 ";
}
else {
    $MSGQuery = " plan.`MSG` >= $musage ";
}
echo $planquery="
                    SELECT plan.*, details.* FROM plan
                    LEFT JOIN details ON plan.`Name` = details.`Name` 
                    WHERE plan.`Phone` = '$device'
                    AND plan.`SIMTYPE` = '$plantype'
                    AND plan.`DATA` >= $dusage
                    AND plan.`CALL` >= $cusage 
                    AND $MSGQuery ";
$planresult=mysql_query($planquery) 
    or die ("Query to get data from firsttable failed: ".mysql_error());
while ( $prow = mysql_fetch_assoc($planresult) ) {

1 个答案:

答案 0 :(得分:0)

使用条件:

...
AND (plan.`DATA` >= $dusage OR plan.`DATA` = -1)
...

不要忘记括号,因为AND的优先级高于OR。