mysql中的条件查询

时间:2013-01-30 11:26:45

标签: php mysql

我正在创建一个条件搜索,它将依赖于大约9个参数。现在我只使用4,它已经变得非常大而且维持繁忙。有人可以建议最好的方法,以便我可以缩短我的代码行并轻松维护它。以下是我的代码

if($min_year=="Select" && $max_year=="") {
        if($makes=='All' && $models=='All Models') {
            $select = mysql_query("select models.modelID, models.manufacturerID, models.modelName, manufacturers.manufacturerID, 
                    manufacturers.manufacturerName, manufacturers.manufacturerDetails, vehicles.* from models, manufacturers, 
                    vehicles where  vehicles.manufacturerID=models.manufacturerID and vehicles.modelID=models.modelID 
                    and models.manufacturerID=manufacturers.manufacturerID LIMIT $start, $per_page");
            $select_count = mysql_query("select count(vehicles.modelID) as count from models, manufacturers, 
                    vehicles where vehicles.manufacturerID=models.manufacturerID and vehicles.modelID=models.modelID 
                    and models.manufacturerID=manufacturers.manufacturerID");                                   
        } elseif($makes!='All' && $models=='All Models') {
            $select = mysql_query("select models.modelID, models.manufacturerID, models.modelName, manufacturers.manufacturerID, 
                    manufacturers.manufacturerName, manufacturers.manufacturerDetails,vehicles.* from models, manufacturers, 
                    vehicles where  models.manufacturerID=$makes and vehicles.manufacturerID=models.manufacturerID and 
                    vehicles.modelID=models.modelID and models.manufacturerID=manufacturers.manufacturerID  LIMIT $start, $per_page
                    ");
            $select_count = mysql_query("select count(vehicles.modelID) as count from models, manufacturers, 
                    vehicles where  models.manufacturerID=$makes and vehicles.manufacturerID=models.manufacturerID and 
                    vehicles.modelID=models.modelID and models.manufacturerID=manufacturers.manufacturerID");           

        } elseif($makes=='All' && $models!='All Models') {
            $select = mysql_query("select models.modelID, models.manufacturerID, models.modelName, manufacturers.manufacturerID, 
                    manufacturers.manufacturerName, manufacturers.manufacturerDetails,vehicles.* from models, manufacturers, 
                    vehicles where vehicles.modelID=models.modelID and models.manufacturerID=manufacturers.manufacturerID 
                     LIMIT $start, $per_page"); 
            $select_count =  mysql_query("select count(vehicles.modelID) as count from models, manufacturers, 
                    vehicles where vehicles.modelID=models.modelID and models.manufacturerID=manufacturers.manufacturerID");

        } elseif($makes!='All' && $models!='All Models' && $makes!='select' && $models!='select') {
            $select = mysql_query("select models.modelID, models.manufacturerID, models.modelName, manufacturers.manufacturerID, 
                    manufacturers.manufacturerName, manufacturers.manufacturerDetails, vehicles.* from models, manufacturers, 
                    vehicles where models.modelID=$models and models.manufacturerID=$makes and vehicles.modelID=models.modelID
                     and models.manufacturerID=manufacturers.manufacturerID LIMIT $start, $per_page");
            $select_count = mysql_query("select count(vehicles.modelID) as count from models, manufacturers, 
                    vehicles where models.modelID=$models and models.manufacturerID=$makes and vehicles.modelID=models.modelID
                     and models.manufacturerID=manufacturers.manufacturerID");          
        } else {
            $select = mysql_query("select models.modelID, models.manufacturerID, models.modelName, manufacturers.manufacturerID, 
                    manufacturers.manufacturerName, manufacturers.manufacturerDetails, vehicles.* from models, 
                    manufacturers, vehicles where vehicles.modelID=models.modelID and 
                    models.manufacturerID=manufacturers.manufacturerID LIMIT $start, $per_page");
            $select_count = mysql_query("select count(vehicles.modelID) as count from models, manufacturers, vehicles 
                        where vehicles.modelID=models.modelID and models.manufacturerID=manufacturers.manufacturerID");                 
        }
    } elseif($min_year!="Select" && $max_year==""){
        if($makes=='All' && $models=='All Models') {
            $select = mysql_query("select models.modelID, models.manufacturerID, models.modelName, manufacturers.manufacturerID, 
                    manufacturers.manufacturerName, manufacturers.manufacturerDetails, vehicles.* from models, manufacturers, 
                    vehicles where  vehicles.manufacturerID=models.manufacturerID and vehicles.year>=$min_year and
                    vehicles.modelID=models.modelID and models.manufacturerID=manufacturers.manufacturerID LIMIT $start, $per_page 
                    ");
            $select_count = mysql_query("select count(vehicles.modelID) as count from models, manufacturers, 
                    vehicles where  vehicles.manufacturerID=models.manufacturerID and vehicles.year>=$min_year and
                    vehicles.modelID=models.modelID and models.manufacturerID=manufacturers.manufacturerID");       

        } elseif($makes!='All' && $models=='All Models') {
            $select = mysql_query("select models.modelID, models.manufacturerID, models.modelName, manufacturers.manufacturerID, 
                    manufacturers.manufacturerName, manufacturers.manufacturerDetails, vehicles.* from models, manufacturers, 
                    vehicles where  models.manufacturerID=$makes and vehicles.manufacturerID=models.manufacturerID and 
                    vehicles.year>=$min_year and vehicles.modelID=models.modelID and models.manufacturerID=manufacturers.manufacturerID 
                     LIMIT $start, $per_page");
            $select_count =  mysql_query("select count(vehicles.modelID) as count from models, manufacturers, 
                    vehicles where  models.manufacturerID=$makes and vehicles.manufacturerID=models.manufacturerID and 
                    vehicles.year>=$min_year and vehicles.modelID=models.modelID and models.manufacturerID=manufacturers.manufacturerID 
                    ");         

        } elseif($makes=='All' && $models!='All Models') {
            $select = mysql_query("select models.modelID, models.manufacturerID, models.modelName, manufacturers.manufacturerID, 
                    manufacturers.manufacturerName, manufacturers.manufacturerDetails, vehicles.* from models, manufacturers, 
                    vehicles where vehicles.modelID=models.modelID and vehicles.year>=$min_year and
                     models.manufacturerID=manufacturers.manufacturerID LIMIT $start, $per_page");
            $select_count = mysql_query("select count(vehicles.modelID) as count from models, manufacturers, 
                    vehicles where vehicles.modelID=models.modelID and vehicles.year>=$min_year and
                     models.manufacturerID=manufacturers.manufacturerID");              
        } else {
            $select = mysql_query("select models.modelID, models.manufacturerID, models.modelName, manufacturers.manufacturerID, 
                    manufacturers.manufacturerName, manufacturers.manufacturerDetails,vehicles.* from models, manufacturers, 
                    vehicles where models.modelID=$models and models.manufacturerID=$makes and vehicles.modelID=models.modelID
                     and vehicles.year>=$min_year and models.manufacturerID=manufacturers.manufacturerID LIMIT $start, $per_page");
            $select_count =  mysql_query("select count(vehicles.modelID) as count from models, manufacturers, 
                    vehicles where models.modelID=$models and models.manufacturerID=$makes and vehicles.modelID=models.modelID
                     and vehicles.year>=$min_year and models.manufacturerID=manufacturers.manufacturerID");         
        }       
    } elseif($min_year=="Select" && $max_year!=""){
        if($makes=='All' && $models=='All Models') {
            $select = mysql_query("select models.modelID, models.manufacturerID, models.modelName, manufacturers.manufacturerID, 
                    manufacturers.manufacturerName, manufacturers.manufacturerDetails, vehicles.* from models, manufacturers, 
                    vehicles where  vehicles.manufacturerID=models.manufacturerID and vehicles.year<=$max_year and
                    vehicles.modelID=models.modelID and models.manufacturerID=manufacturers.manufacturerID  LIMIT $start, $per_page
                    ");
            $select_count =  mysql_query("select count(vehicles.modelID) as count from models, manufacturers, 
                    vehicles where  vehicles.manufacturerID=models.manufacturerID and vehicles.year<=$max_year and
                    vehicles.modelID=models.modelID and models.manufacturerID=manufacturers.manufacturerID");       

        } elseif($makes!='All' && $models=='All Models') {
            $select = mysql_query("select models.modelID, models.manufacturerID, models.modelName, manufacturers.manufacturerID, 
                    manufacturers.manufacturerName, manufacturers.manufacturerDetails, vehicles.* from models, manufacturers, 
                    vehicles where  models.manufacturerID=$makes and vehicles.manufacturerID=models.manufacturerID and 
                    vehicles.year<=$max_year and vehicles.modelID=models.modelID and models.manufacturerID=manufacturers.manufacturerID 
                     LIMIT $start, $per_page"); 
            $select_count =  mysql_query("select count(vehicles.modelID) as count from models, manufacturers, 
                    vehicles where  models.manufacturerID=$makes and vehicles.manufacturerID=models.manufacturerID and 
                    vehicles.year<=$max_year and vehicles.modelID=models.modelID and models.manufacturerID=manufacturers.manufacturerID 
                    ");      

        } elseif($makes=='All' && $models!='All Models') {
            $select = mysql_query("select models.modelID, models.manufacturerID, models.modelName, manufacturers.manufacturerID, 
                    manufacturers.manufacturerName, manufacturers.manufacturerDetails, vehicles.* from models, manufacturers, 
                    vehicles where vehicles.modelID=models.modelID and vehicles.year<=$max_year and
                     models.manufacturerID=manufacturers.manufacturerID LIMIT $start, $per_page");
            $select_count =  mysql_query("select count(vehicles.modelID) as count from models, manufacturers, 
                    vehicles where vehicles.modelID=models.modelID and vehicles.year<=$max_year and
                     models.manufacturerID=manufacturers.manufacturerID");              
        } else {
            $select = mysql_query("select models.modelID, models.manufacturerID, models.modelName, manufacturers.manufacturerID, 
                    manufacturers.manufacturerName, manufacturers.manufacturerDetails, vehicles.* from models, manufacturers, 
                    vehicles where models.modelID=$models and models.manufacturerID=$makes and vehicles.modelID=models.modelID
                     and vehicles.year<=$max_year and models.manufacturerID=manufacturers.manufacturerID LIMIT $start, $per_page");
           $select_count = mysql_query("select count(vehicles.modelID) as count from models, manufacturers, 
                    vehicles where models.modelID=$models and models.manufacturerID=$makes and vehicles.modelID=models.modelID
                     and vehicles.year<=$max_year and models.manufacturerID=manufacturers.manufacturerID");             
        }       
    } else {
        if($makes=='All' && $models=='All Models') {
            $select = mysql_query("select models.modelID, models.manufacturerID, models.modelName, manufacturers.manufacturerID, 
                    manufacturers.manufacturerName, manufacturers.manufacturerDetails, vehicles.* from models, manufacturers, 
                    vehicles where  vehicles.manufacturerID=models.manufacturerID and vehicles.year<=$max_year and vehicles.year>=$min_year
                     and vehicles.modelID=models.modelID and models.manufacturerID=manufacturers.manufacturerID LIMIT $start, $per_page 
                    ");
            $select_count = mysql_query("select count(vehicles.modelID) as count from models, manufacturers, 
                    vehicles where  vehicles.manufacturerID=models.manufacturerID and vehicles.year<=$max_year and vehicles.year>=$min_year
                     and vehicles.modelID=models.modelID and models.manufacturerID=manufacturers.manufacturerID");      

        } elseif($makes!='All' && $models=='All Models') {
            $select = mysql_query("select models.modelID, models.manufacturerID, models.modelName, manufacturers.manufacturerID, 
                    manufacturers.manufacturerName, manufacturers.manufacturerDetails, vehicles.* from models, manufacturers, 
                    vehicles where  models.manufacturerID=$makes and vehicles.manufacturerID=models.manufacturerID and 
                    vehicles.year<=$max_year and vehicles.year>=$min_year and vehicles.modelID=models.modelID 
                    and models.manufacturerID=manufacturers.manufacturerID LIMIT $start, $per_page ");  
            $select_count = mysql_query("select count(vehicles.modelID) as count from models, manufacturers, 
                    vehicles where  models.manufacturerID=$makes and vehicles.manufacturerID=models.manufacturerID and 
                    vehicles.year<=$max_year and vehicles.year>=$min_year and vehicles.modelID=models.modelID 
                    and models.manufacturerID=manufacturers.manufacturerID");       

        } elseif($makes=='All' && $models!='All Models') {
            $select = mysql_query("select models.modelID, models.manufacturerID, models.modelName, manufacturers.manufacturerID, 
                    manufacturers.manufacturerName, manufacturers.manufacturerDetails, vehicles.* from models, manufacturers, 
                    vehicles where vehicles.modelID=models.modelID and vehicles.year<=$max_year and vehicles.year>=$min_year and
                     models.manufacturerID=manufacturers.manufacturerID LIMIT $start, $per_page");
            $select_count = mysql_query("select count(vehicles.modelID) as count from models, manufacturers, 
                    vehicles where vehicles.modelID=models.modelID and vehicles.year<=$max_year and vehicles.year>=$min_year and
                     models.manufacturerID=manufacturers.manufacturerID");              
        } else {
            $select = mysql_query("select models.modelID, models.manufacturerID, models.modelName, manufacturers.manufacturerID, 
                    manufacturers.manufacturerName, manufacturers.manufacturerDetails, vehicles.* from models, manufacturers, 
                    vehicles where models.modelID=$models and models.manufacturerID=$makes and vehicles.modelID=models.modelID
                     and vehicles.year<=$max_year and vehicles.year>=$min_year and models.manufacturerID=manufacturers.manufacturerID 
                      LIMIT $start, $per_page");
            $select_count = mysql_query("select count(vehicles.modelID) as count from models, manufacturers, 
                    vehicles where models.modelID=$models and models.manufacturerID=$makes and vehicles.modelID=models.modelID
                     and vehicles.year<=$max_year and vehicles.year>=$min_year and models.manufacturerID=manufacturers.manufacturerID 
                     ");            
        }       
    }

正如您所看到的,这些循环越来越复杂。请提出建议。提前致谢

2 个答案:

答案 0 :(得分:1)

让你的sql更通用,而不是预先建立每个案例。 只是一个简单的例子:

$strQuery = 'select 
 models.modelID
,models.manufacturerID
,models.modelName
,manufacturers.manufacturerID
,manufacturers.manufacturerName
,manufacturers.manufacturerDetails
,vehicles.*
from 
models
,manufacturers
,vehicles';

$aWhere = array();
if($min_year != "Select")
{
    array_push($aWhere,'vehicles.year >= ' . $min_year);
}
if($makes != 'All')
{
    array_push($aWhere,'models.manufacturerID=' . $makes);
}

if(count($aWhere) > 0)
{
    $strQuery = $strQuery . ' WHERE ';
}
for($i = 0;$i < count($aWhere);$i++)
{
    if($i == 0)
    {
        $strQuery .= $aWhere[$i];
    }
    else
    {
        $strQuery .= ' AND ' . $aWhere[$i];
    }
}

// Append the other things like order by or limit ...

PS:别忘了逃避你的sql-query变量!

答案 1 :(得分:0)

你的代码真的很复杂。据我了解,您有9个条件来显示不同的查询。

假设,这条件每个都会添加一条查询。

现在,您检查条件,然后选择适当的查询。为什么不根据每个条件逐个构建查询?