2013-10-07 267 views
0

我試圖讓這樣的查詢:MySQL查詢多條件

$query= "SELECT a.virtuemart_category_id, 
    a.virtuemart_product_id, 
    b.product_name, 
    b.product_desc, 
    c.product_price, 
    d.virtuemart_media_id, 
    e.file_url 
    FROM wgny6_virtuemart_product_categories a 
    LEFT JOIN wgny6_virtuemart_products_en_gb b 
      ON a.virtuemart_product_id = b.virtuemart_product_id 
    LEFT JOIN wgny6_virtuemart_product_prices c 
      ON b.virtuemart_product_id = c.virtuemart_product_id 
    LEFT JOIN wgny6_virtuemart_product_medias d 
      ON b.virtuemart_product_id = d.virtuemart_product_id 
    LEFT JOIN wgny6_virtuemart_medias e 
      ON d.virtuemart_media_id = e.virtuemart_media_id 
    WHERE b.product_name IS NOT NULL"; 

但我想會出現這種情況只有在a.virtuemart_category_id = $ ID。但我無法弄清楚我將如何補充這種情況。

回答

1

簡單的說多餘的地方在您的查詢的結束條件

AND a.virtuemart_category_id = $id 

SELECT a.virtuemart_category_id,a.virtuemart_product_id,b.product_name,b.product_desc, c.product_price,d.virtuemart_media_id,e.file_url 
    FROM wgny6_virtuemart_product_categories a 
     LEFT JOIN wgny6_virtuemart_products_en_gb b ON a.virtuemart_product_id=b.virtuemart_product_id 
     LEFT JOIN wgny6_virtuemart_product_prices c ON b.virtuemart_product_id=c.virtuemart_product_id 
     LEFT JOIN wgny6_virtuemart_product_medias d ON b.virtuemart_product_id=d.virtuemart_product_id 
     LEFT JOIN wgny6_virtuemart_medias e ON d.virtuemart_media_id=e.virtuemart_media_id 
    WHERE b.product_name IS NOT NULL AND a.virtuemart_category_id = $id 
+0

確定感謝...我把這樣AND a.virtuemart_category_id = '$ ID' 可能是這樣的,爲什麼它不工作。但所以它會沒有冒號只是$ id ...謝謝 – jibon57

+0

@ jibon57高興地幫助你jibon :) –

1
SELECT ... 
FROM ... 
WHERE b.product_name IS NOT NULL 
     AND a.virtuemart_category_id= $id // <----------------------------------- 

如果您使用PHP,請確保您使用綁定參數和準備好的語句。