连接具有空字段值的表时出错

时间:2015-03-30 12:31:02

标签: mysql

我有一个名为product_data的主表,它链接到另一个名为attrib_category的表。

attrib_category表包含字段idnamesubcat。每条记录都有一个idname,但有些记录没有为subcat设置值,我注意到当我加入attrib_category的记录时没有prouct_data值设置的subcat会抛出处理该记录的错误。

SELECT
  product_data.*,           
  attrib_categorymain.id AS attrib_categorymain_id,
  attrib_categorymain.name AS attrib_categorymain_name,
  attrib_categorymain.subcat AS subcat            
FROM product_data
LEFT JOIN attrib_categorymain ON attrib_categorymain.id = product_data.attrib_categorymain

1 个答案:

答案 0 :(得分:0)

试试这个

SELECT product_data.*,
            attrib_categorymain.id                  AS attrib_categorymain_id,
            attrib_categorymain.name                AS attrib_categorymain_name,
            attrib_categorymain.subcat              AS subcat



    FROM product_data product_data
        LEFT JOIN attrib_category attrib_categorymain 
            ON attrib_categorymain.id = product_data.attrib_categorymain