将单个特定产品添加到sql查询中

时间:2017-01-21 17:09:43

标签: sql

我想在此查询中添加单个产品products_id = 886:

SELECT p.products_id, 
       p.products_model, 
       pd.products_name, 
       p.products_price, 
       p.products_tax_class_id, 
       p.products_image, 
       s.specials_new_products_price, 
       IF(s.status, s.msrp_price, 0)    AS msrp_price, 
       IF(s.status, s.expires_date, '') AS expiry_date 
FROM   products p, 
       products_description pd, 
       products_to_categories p2c, 
       specials s 
WHERE  p.products_status = '1' 
       AND s.products_id = p.products_id 
       AND p.products_id = pd.products_id 
       AND pd.language_id = '1' 
       AND p2c.products_id = p.products_id 
       AND p2c.categories_id IN ( 23, 75, 45, 46, 
                                  47, 48 ) 
       AND s.status = '1' 
ORDER  BY s.specials_sort_order, 
          pd.products_name 

我仍然希望查询将所有产品保留在查询类别Ids中,只需添加上面提到的单个产品ID即可。这是可能的,还是我需要一个单独的查询并以某种方式加入它们?

results currently

我想添加1行(1个产品),不在类别列表中,它属于不同的类别

1 个答案:

答案 0 :(得分:0)

如果您需要单行添加,可以使用UNION

SELECT p.products_id, 
       p.products_model, 
       pd.products_name, 
       p.products_price, 
       p.products_tax_class_id, 
       p.products_image, 
       s.specials_new_products_price, 
       IF(s.status, s.msrp_price, 0)    AS msrp_price, 
       IF(s.status, s.expires_date, '') AS expiry_date 
FROM   products p, 
       products_description pd, 
       products_to_categories p2c, 
       specials s 
WHERE  p.products_status = '1' 
       AND s.products_id = p.products_id 
       AND p.products_id = pd.products_id 
       AND pd.language_id = '1' 
       AND p2c.products_id = p.products_id 
       AND p2c.categories_id IN ( 23, 75, 45, 46, 
                                  47, 48 ) 
       AND s.status = '1' 

UNION 

    SELECT p.products_id, 
       p.products_model, 
       pd.products_name, 
       p.products_price, 
       p.products_tax_class_id, 
       p.products_image, 
       s.specials_new_products_price, 
       IF(s.status, s.msrp_price, 0)    AS msrp_price, 
       IF(s.status, s.expires_date, '') AS expiry_date 
FROM   products p, 
       products_description pd, 
       products_to_categories p2c, 
       specials s 
WHERE  p.products_status = '1' 
       AND s.products_id = p.products_id 
       AND p.products_id = pd.products_id 
       AND pd.language_id = '1' 
       AND p2c.products_id = p.products_id 
       AND s.status = '1' 
       AND p.products_id = 'YOUR PRODUCT CODE'
ORDER  BY s.specials_sort_order, 
          pd.products_name