2014-05-02 45 views
0

我有一個表有4個類別列,下面返回第一個category_id。我有3個人稱爲category_id2,category_id3和Category_id4。包含SQL和PHP的多列

$sql_ca = "SELECT * FROM tour_category ORDER BY sort"; 
         $result_ca = mysql_query($sql_ca); 
         $categories = $Manager->fetchAssoc($result_ca); 
         if(!empty($categories)){ 
          foreach($categories as $cat){ 

           $sql_tour = "SELECT t.* FROM tour t 
           WHERE 
            t.touronline = 'yes' 
            AND t.category_id = {$cat['id']} 
            AND t.location_id = {$city['id']} 
            and t.type = 'P' 
           ORDER BY t.tourcode     
           "; 
           $result_tour = mysql_query($sql_tour); 
           $tours = $Manager->fetchAssoc($result_tour); 

我試圖把在

AND t.category_id, t.category_id2, t.category_id3,t.category_id4 = {$cat['id']} 

但我只是無法得到這個工作,任何提示?

+0

使用'= {$貓[ 'id']}'爲每列。 – Mihai

+0

嘗試回顯查詢並將其複製並手動檢查 –

回答

0

您需要每個列的WHERE條件。如果您希望所有類別列匹配$cat['id']它應該是這樣的:

WHERE 
    t.touronline = 'yes' 
    AND t.category_id = {$cat['id']} 
    AND t.category_id2 = {$cat['id']} 
    AND t.category_id3 = {$cat['id']} 
    AND t.category_id4 = {$cat['id']} 
    AND t.location_id = {$city['id']} 
    AND t.type = 'P' 

如果你想要的任何類別列匹配$cat['id']它應該是這樣的:

WHERE 
    t.touronline = 'yes' 
    AND (t.category_id = {$cat['id']} 
     OR t.category_id2 = {$cat['id']} 
     OR t.category_id3 = {$cat['id']} 
     OR t.category_id4 = {$cat['id']}) 
    AND t.location_id = {$city['id']} 
    AND t.type = 'P' 
+0

是的,這很好地工作。謝謝 – user3477311