-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。如何更改查询以设置为真?

4

3 回答 3

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 
于 2013-07-26T10:07:19.677 回答
0

您可以添加查询以确保 ps_image 不为空

i.id_product 不为空

于 2013-07-26T10:05:18.790 回答
0

您应该将连接与过滤器分开,然后使用 aleft 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 ....
于 2013-07-26T10:06:13.590 回答