2013-11-23 103 views
0

我正在使用一些過濾器來顯示產品。像顏色,價格和東西過濾器。查詢操作數據庫

鏈接:http://www.applechain.com/products/iPod.php

我用這個查詢

$sql = "Select * 
    from tbl_product 
    where device='iPhone' 
    and (color='$c1' 
     or color='$c2'  
     or color='$c3'  
     or color='$c4'  
     or color='$c5'  
     or color='$c6'  
     or color='$c7'  
     or color='$c8'  
     or color='$c9'  
     or color='$c10' 
    ) and (storage='$cp1'  
     or storage='$cp2'  
     or storage='$cp3'  
     or storage='$cp4'  
     or storage='$cp5' 
    ) and (f_unlock='$factory')  
    and (warranty='$warranty')  
    and (price >= '$price1'  
     and price <= '$price2' 
    ) 
    order by product_id desc"; 

現在的事情是,我想說明,即使所有的疑問都不滿意的結果。如果只有幾個選項相匹配,我想在此基礎上顯示結果,那麼如何?

我會感謝您的幫助。

+0

然後你想要「或」,而不是「和」。取而代之,你應該很好。 –

回答

0

嘗試這樣的查詢:

$sql = " 
    SELECT 
     *, 
     1 as order_prority 
    FROM tbl_product 
    WHERE 
     device = 'iPhone' 
     AND color IN ('$c1', '$c2', '$c3', '$c4', '$c5', 
      '$c6', '$c7', '$c8', '$c9', '$c10') 
     AND storage IN ('$cp1', '$cp2', '$cp3', '$cp4', '$cp5') 
     AND f_unlock = '$factory' 
     AND warranty = '$warranty' 
     AND (price BETWEEN '$price1' AND '$price2') 
    UNION SELECT 
     *, 
     2 
    FROM tbl_product 
    WHERE 
     device = 'iPhone' 
     AND (
      color IN ('$c1', '$c2', '$c3', '$c4', '$c5', 
         '$c6', '$c7', '$c8', '$c9', '$c10') 
      OR storage IN ('$cp1', '$cp2', '$cp3', '$cp4', '$cp5') 
      OR f_unlock = '$factory' 
      OR warranty = '$warranty' 
      OR (price BETWEEN '$price1' AND '$price2') 
     ) 
    ORDER BY order_prority ASC, product_id DESC 
"; 

在所有情況下,您會收到一些效果。如果第一個SELECT(更嚴格)返回某些內容,則數據將包含頂部的結果,第二個SELECT(更軟)將最終返回。如果第一個SELECT將不返回任何內容,則將從第二個軟查詢中獲取數據。

1
$sql = "SELECT *  
     FROM tbl_product 
     WHERE device = 'iPhone' 
     AND color IN ('$c1', '$c2', '$c3', '$c4', '$c5', 
      '$c6', '$c7', '$c8', '$c9', '$c10') 
     AND storage IN ('$cp1', '$cp2', '$cp3', '$cp4', '$cp5') 
     AND f_unlock = '$factory' 
     AND warranty = '$warranty' 
     AND ((price >= '$price1') AND (price <= '$price2')) 
     order by product_id desc";