MYSQL GROUP_CONCAT CONCAT_WS并处理NULL值

时间:2013-01-30 23:02:34

标签: mysql null case group-concat concat-ws

如何从这里处理具有NULL值的行(当值为NULL时,这只是一个位置,因为产品没有任何属性):

GROUP_CONCAT(DISTINCT (CONCAT_WS (': ', GroupAttrLang.`name`, AttrLang.`name`)) ORDER BY AttrLang.`name` ASC

这是我的BIG查询的一部分:

SELECT
TProduct.`reference` AS ProdReference,
ProdLang.`name` AS ProductName,
ProdLang.`description` AS ProdDesc,
ROUND((TProduct.`price`*1.23)/4.1989,2) AS ProductPrice,

CASE 
    WHEN COUNT(ProdAttr.`id_product`) = 0 THEN 1 
    ELSE (GROUP_CONCAT(DISTINCT (CONCAT_WS (': ', GroupAttrLang.`name`, AttrLang.`name`)) ORDER BY AttrLang.`name` ASC)) 
END AS ProdAttributes,

/*IF ((COUNT(ProdAttr.`id_product`) > 0), GROUP_CONCAT(DISTINCT (CONCAT_WS (': ', GroupAttrLang.`name`, AttrLang.`name`)) ORDER BY AttrLang.`name` ASC) , 'n/a' ) AS ProdAttributes,*/

GROUP_CONCAT(DISTINCT (CONCAT_WS (' - ', ParLang.`name`, CatLang.`name` ))) AS ProdFit 
    FROM `ps_product` AS TProduct
        LEFT JOIN `ps_category_product` AS CatProd ON CatProd.`id_product` = TProduct.`id_product`

        LEFT JOIN `ps_category` AS Cat ON Cat.`id_category` = CatProd.`id_category`
        LEFT JOIN `ps_category_lang` AS ParLang ON Cat.`id_parent` = ParLang.`id_category`
        LEFT JOIN `ps_product_lang` AS ProdLang ON ProdLang.`id_product` = TProduct.`id_product`
        LEFT JOIN `ps_category_lang` AS CatLang ON CatLang.`id_category` = CatProd.`id_category`
        LEFT JOIN `ps_product_attribute` AS ProdAttr ON ProdAttr.`id_product` = TProduct.`id_product`
        LEFT JOIN `ps_product_attribute_combination` AS ProdAttrComb ON ProdAttrComb.`id_product_attribute` = ProdAttr.`id_product_attribute`
        LEFT JOIN `ps_attribute` AS TAttr ON TAttr.`id_attribute` = ProdAttrComb.`id_attribute`
        LEFT JOIN `ps_attribute_group_lang` AS GroupAttrLang ON GroupAttrLang.`id_attribute_group` = TAttr.`id_attribute_group`
        LEFT JOIN `ps_attribute_lang` AS AttrLang ON AttrLang.`id_attribute` = TAttr.`id_attribute`
    WHERE 
        Cat.`id_parent` != 1 AND 
        ProdLang.`id_lang`=1 AND
        CatLang.`id_lang` = 1 AND
        GroupAttrLang.`id_lang` = 1 AND
        AttrLang.`id_lang` = 1
             GROUP BY TProduct.`reference` ORDER BY TProduct.`reference` ASC;

所有工作除了处理上面列表中的NULL值之外。我试过了 IF 语句结果相同,查询跳过具有NULL值的行。 我尝试制作一个只打印一件事的查询:

SELECT 
CASE 
    WHEN COUNT(ProdAttr.`id_product`) > 0 THEN COUNT(ProdAttr.`id_product`)
    ELSE 'n/a'
END AS ProdAttrList
FROM `ps_product` AS TProduct 
    LEFT JOIN `ps_product_attribute` AS ProdAttr ON ProdAttr.`id_product`=TProduct.`id_product` 
        GROUP BY TProduct.`reference`;

工作完美,请参阅图片PrtSc. of last listing

1 个答案:

答案 0 :(得分:0)

必须更改 WHERE 声明:

...WHERE
GroupAttrLang.`id_lang` = 1 AND
AttrLang.`id_lang` = 1...

对此:

LEFT JOIN `ps_attribute_group_lang` AS GroupAttrLang ON GroupAttrLang.`id_attribute_group` = TAttr.`id_attribute_group` AND GroupAttrLang.`id_lang` = 1
LEFT JOIN `ps_attribute_lang` AS AttrLang ON AttrLang.`id_attribute` = TAttr.`id_attribute` AND AttrLang.`id_lang` = 1 

问题已解决,您现在可以从prestashop导出产品目录。