MYSQL GROUP_CONCAT CONCAT_WS 并处理 NULL 值
Posted
技术标签:
【中文标题】MYSQL GROUP_CONCAT CONCAT_WS 并处理 NULL 值【英文标题】:MYSQL GROUP_CONCAT CONCAT_WS and handle NULL values 【发布时间】:2013-01-14 22:46:25 【问题描述】:如何处理具有 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】:必须改变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 中导出产品目录。
【讨论】:
以上是关于MYSQL GROUP_CONCAT CONCAT_WS 并处理 NULL 值的主要内容,如果未能解决你的问题,请参考以下文章
mysql函数concat与group_concat使用说明