如何选择与其他列匹配的更多结果?

时间:2014-01-24 15:45:35

标签: mysql sql

我想知道是否可以从其他列的列中选择更多结果:

  public function SameCars() {
    $q = "select * from carads where STATUS='1' and DEL='0' and ADID=".$_REQUEST['carID'];
    $result = $this->QueryResult($q);
    $myprice = intval($result[0]->PRICE);
    $body = $result[0]->BODY;
    $pricelow = $myprice - (30/100 * $myprice);
    $pricehigh = $myprice + (30/100 * $myprice);
    $myyear= $result[0]->YEAR;
    $currentyear = date('Y');
    $yearlow = $myyear - 5;
    if ($yearhigh <= $currentyear-3) {
    $yearhigh = $myyear + 5;
    } else { $yearhigh = $myyear; }
    $samecars = $this->QueryResult("SELECT * FROM carads where BODY LIKE '$body' AND STATUS='1' AND YEAR BETWEEN $yearlow and $yearhigh and DEL='0' and TITLE !='' and TITLE !='-1' and IMAGE1 != '' and IMAGE1 != '-1' and PRICE BETWEEN $pricelow and $pricehigh LIMIT 0,3");
    $i=0;
    if (count($samecars)) {
        foreach ($samecars as $samecar) {$i++;
        if($i==3) $cls="news-row last"; else $cls="news-row";
        echo '<div class="'.$cls.'">
                    <div class="img"><a href="'.$base_path.'detail-page_'.$samecar->ADID.'.html">';

                    if($samecar->IMAGE1!='' && !empty($samecar))
                    echo '<img src="'.$samecar->IMAGE1.'" alt="" height="79" width="89" />';
                    else echo '<img src="'.$base_path.'uploads/no-img.jpg" height="79" width="89"/>';
                    echo '</a></div>
                    <div class="details">
                    <h5><a href="'.$base_path.'detail-page_'.$samecar->ADID.'.html">'.$samecar->TITLE.'</a></h5>
                    <div class="descn">"'.$this->FckContentView($this->closetags(substr($samecar->DESCRIPT, 0,50))).'</div>
                    </div>
                </div>';
        }
    }
}

我希望$make比其他标准具有60%的重要性。

例如,在此查询中,它将从匹配这些条件的数据库中回显来自MAKE“BMW”的前3辆车的3个拇指的列表。我想输出3个拇指;来自宝马的2个拇指和来自另一辆车的1个拇指在标准范围内但不同MAKE

1 个答案:

答案 0 :(得分:1)

SELECT * FROM carads 
WHERE BODY LIKE '$body' 
AND MAKE = '$make'
AND STATUS='1' 
AND YEAR BETWEEN $yearlow and $yearhigh 
AND DEL='0' 
AND TITLE !='' 
AND TITLE !='-1' 
AND IMAGE1 != '' 
AND IMAGE1 != '-1' 
AND PRICE BETWEEN $pricelow and $pricehigh LIMIT 0,2

然后使用

运行第二个查询
SELECT * FROM carads 
WHERE BODY LIKE '$body'
AND MAKE <> '$make' 
AND STATUS='1' 
AND YEAR BETWEEN $yearlow and $yearhigh 
AND DEL='0' 
AND TITLE !='' 
AND TITLE !='-1' 
AND IMAGE1 != '' 
AND IMAGE1 != '-1' 
AND PRICE BETWEEN $pricelow and $pricehigh LIMIT 0,1

尝试使用单个查询执行此操作是可能但非常混乱,我建议2个查询 - LIMIT将确保您不会对数据库施加过多负载