2013-07-26 33 views
-1

這是我的查詢以查詢真當一個條款是假

SELECT pl.NAME 
    ,pl.id_product 
    ,pl.link_rewrite 
    ,i.id_image 
    ,ar.id_auctions_rutcom 
    ,ar.actual_bid_price 
    ,ar.start_price 
    ,ar.min_price 
    ,ar.buy_now_price 
    ,ar.finish_date 
FROM ps_image i 
    ,ps_auctions_rutcom ar 
    ,ps_product_lang pl 
WHERE ar.finish_date > '2013-07-26 11:18:15' 
    AND ar.start_date < '2013-07-26 11:18:15' 
    AND ar.active = 1 
    AND ar.finish = 0 
    AND pl.id_product = ar.id_product 
    AND i.id_product = ar.id_product 
    AND i.cover = 1 
    AND pl.id_lang = 6 

我有問題,當上表ps_image沒有圖像,其中i.id_product = ar.id_product我的查詢返回false。如何更改查詢設置爲true?

+0

你必須更多地解釋與你有什麼,你喜歡什麼結果讓 – FSou1

+0

你是說要檢索德數據的其餘部分,即使你沒有一個形象的例子嗎? –

+0

請參閱[踢壞的習慣:使用舊式JOIN](http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/08/bad-habits-to-kick-using-old-style-joins的.aspx)。 –

回答

2

你應該瞭解JOIN。去谷歌上查詢。

你的查詢等效於INNER JOIN

SELECT 
    pl.name, pl.id_product, pl.link_rewrite, i.id_image, ar.id_auctions_rutcom, 
    ar.actual_bid_price, ar.start_price, ar.min_price, ar.buy_now_price, ar.finish_date 
FROM ps_auctions_rutcom ar 
INNER JOIN ps_product_lang pl 
     ON pl.id_product = ar.id_product 
     AND pl.id_lang = 6 
INNER JOIN ps_image i 
     ON i.id_product = ar.id_product 
     AND i.cover = 1 
WHERE ar.finish_date > '2013-07-26 11:18:15' 
    AND ar.start_date < '2013-07-26 11:18:15' 
    AND ar.active = 1 
    AND ar.finish = 0 

隨着INNER JOIN,如果在相關表中沒有匹配行,從結果集中省略行。

現在,您應該使用OUTER JOIN,在這種情況下該行仍在結果集中,缺少的信息設置爲NULL

SELECT 
    pl.name, pl.id_product, pl.link_rewrite, i.id_image, ar.id_auctions_rutcom, 
    ar.actual_bid_price, ar.start_price, ar.min_price, ar.buy_now_price, ar.finish_date 
FROM ps_auctions_rutcom ar 
INNER JOIN ps_product_lang pl 
     ON pl.id_product = ar.id_product 
     AND pl.id_lang = 6 
LEFT OUTER JOIN ps_image i 
      ON i.id_product = ar.id_product 
      AND i.cover = 1 
WHERE ar.finish_date > '2013-07-26 11:18:15' 
    AND ar.start_date < '2013-07-26 11:18:15' 
    AND ar.active = 1 
    AND ar.finish = 0 
0

您可以在查詢中添加,以確保ps_image有沒有空

凡i.id_product IS NOT NULL

0

你應該分開你從你的過濾器連接,然後使用一個left join提供的所有結果 不管圖像是否存在

FROM ps_auctions_rutcom ar 
    left join 
    ps_image i on i.id_product = ar.id_product 
     and i.cover = 1 
    left join 
    ps_product_lang pl on pl.id_product = ar.id_product 
     and pl.id_lang = 6 
WHERE ....