2014-02-28 58 views
2

我有以下查詢。如果子查詢返回空值,如何刪除一行?

select 
     Product.*, 
     (
     select 
       group_concat(features.feature_image order by product_features.feature_order) 
      from product_features 
      inner join features 
       on features.id = product_features.feature_id 
      where 
       product_features.product_id = Product.id 
       and product_features.feature_id in(1) 
     ) feature_image 
    from products as Product 
    where 
     Product.main_product_id=1 
     and Product.product_category_id='1' 

如果feature_image爲空,我想繞過該行。

+0

使用Where子句where feature_image <> null –

回答

1

你的查詢看起來有點奇怪,因爲你正在做的大多數子查詢的工作:

select p.*, (select group_concat(f.feature_image order by pf.feature_order) 
      from product_features pf inner join 
        features f 
        on f.id = pf.feature_id 
        where pf.product_id = p.id and pf.feature_id in (1) 
      ) as feature_image 
from products p 
where p.main_product_id=1 and p.product_category_id='1'; 

詞組查詢是在外部查詢的inner join更常見的方式:

select p.*, group_concat(f.feature_image order by pf.feature_order) as feature_image 
from products p join 
    product_features pf 
    on pf.product_id = p.id and pf.feature_id in (1) join 
    features f 
    on f.id = pf.feature_id 
where p.main_product_id=1 and p.product_category_id='1' 
group by p.id; 

這將只會自動包含具有匹配功能的產品。您將使用left outer join獲取所有產品。