2013-10-11 133 views
0

我有簡單的MySQL查詢:IF和LEFT JOIN不工作

SELECT product.user_id, product.type, product.body, product.created, product.id, product.url, product.name, product.price, images.src 
FROM xn_product AS product 
LEFT JOIN xn_images AS images ON product.id = images.item_id 
WHERE product.active = '1' AND images.type = 'product' AND product.promoted = '1' AND images.main = '1' AND product.url = '". $url "' 

而且我有問題。在images.table中沒有product.id查詢時不執行。我試圖用IF和IF NULL做一些事情,但沒有成功。

我想看看它是否是空的images.src覆蓋它們爲「empty.jpg」 OR 1

回答

0

嘗試使用內部連接

SELECT product.user_id, product.type, product.body, product.created, product.id, product.url, product.name, product.price, images.src 
FROM xn_product AS product 
INNER JOIN xn_images AS images ON product.id = images.item_id 
WHERE product.active = '1' AND images.type = 'product' AND product.promoted = '1' AND images.main = '1' AND product.url = '". $url ."' 
+0

「WHERE」我改變它的問題AND並正常工作,並只給AND product.url ='「。$ url」'WHERE :) –