MySql If,Left Join Trouble

时间:2014-12-23 17:15:47

标签: mysql

这是我的查询

select 
    p.products_id, p.products_type, p.master_categories_id, p.manufacturers_id, 
    p.products_price, p.products_tax_class_id, p.products_priced_by_attribute, 
    pd.products_description, pd.products_special_data, IF(s.status = 1, 
    s.specials_new_products_price, NULL) as specials_new_products_price,
    IF(p.products_priced_by_attribute = 1, pa.options_values_price, 
        IF(s.status = 1, s.specials_new_products_price, p.products_price) 
    ) as final_price, 
    p.product_is_call, p.product_is_always_free_shipping, p.products_qty_box_status 
from products p 
left join specials s on p.products_id = s.products_id 
left join products_attributes pa on p.products_id = pa.products_id, 
products_description pd, 
manufacturers m, 
products_to_categories p2c 
where p.products_status = 1 
    and p.manufacturers_id = m.manufacturers_id 
    and p.products_id = p2c.products_id 
    and pd.products_id = p2c.products_id 
    and p.products_price >= 221 
    and p.products_price <= 815 
    and pd.language_id = '1' 
    and p2c.categories_id = '630'

产品结构表:

products_id     products_type   products_quantity   products_model  products_image  products_price  products_virtual    products_date_added     products_last_modified  products_date_available     products_weight     products_status     products_tax_class_id   manufacturers_id    products_ordered    products_quantity_order_min     products_quantity_order_units   products_priced_by_attribute    product_is_free     product_is_call     products_quantity_mixed     product_is_always_free_shipping     products_qty_box_status     products_quantity_order_max     products_sort_order     products_discount_type  products_discount_type_from     products_price_sorter   master_categories_id    products_mixed_discount_quantity    metatags_title_status   metatags_products_name_status   metatags_model_status   metatags_price_status   metatags_title_tagline_status   qbi_imported    attributePosition   supplier    flat_rate_shipable  products_upc    products_condition 

products_attributes表的结构:

products_attributes_id  products_id     options_id  options_values_id   options_values_price    price_prefix    products_options_sort_order     product_attribute_is_free   products_attributes_weight  products_attributes_weight_prefix   attributes_display_only     attributes_default  attributes_discounted   attributes_image    attributes_price_base_included  attributes_price_onetime    attributes_price_factor     attributes_price_factor_offset  attributes_price_factor_onetime     attributes_price_factor_onetime_offset  attributes_qty_prices   attributes_qty_prices_onetime   attributes_price_words  attributes_price_words_free     attributes_price_letters    attributes_price_letters_free   attributes_required     minimum_quantity

问题是我有四种产品的结果,应该有五种产品。缺少的那个是p.products_priced_by_attribute等于1的那个。我无法判断它是否没有被返回,因为它在pov中实际上有3行表格,它需要限制为1或者如果我根本没有与pov表格正确相关。

1 个答案:

答案 0 :(得分:0)

如果是我,我会从以下开始,然后围绕这个问题构建我的问题......

SELECT p.products_id
     , p.products_type
     , p.master_categories_id
     , p.manufacturers_id
     , p.products_price
     , p.products_tax_class_id
     , p.products_priced_by_attribute
     , pd.products_description
     , pd.products_special_data
     , pa.options_id
     , pa.options_values_price
     , IF(s.status = 1, s.specials_new_products_price, NULL) specials_new_products_price
     , IF(p.products_priced_by_attribute = 1, pa.options_values_price, IF(s.status = 1, s.specials_new_products_price, p.products_price)) final_price
     , p.product_is_call
     , p.product_is_always_free_shipping
     , p.products_qty_box_status
  FROM products p 
  LEFT
  JOIN specials s
    ON s.products_id = p.products_id
  LEFT 
  JOIN products_attributes pa 
    ON pa.products_id = p.products_id 
  JOIN products_to_categories p2c
    ON p2c.products_id = p.products_id 
  JOIN products_description pd
    ON pd.products_id = p2c.products_id
  JOIN manufactureres m
    ON m.manufacturers_id = p.manufacturers_id 
 WHERE p.products_status = 1
   AND pd.language_id = $_SESSION['languages_id']
   AND p2c.categories_id = $current_category_id;