Задавайте вопросы, мы ответим
Вы не зашли.
Есть такой запрос (рабочий)
SELECT PCP.`id_product`,
PPL.`name`,
PP.`reference` AS 'артикул',
GROUP_CONCAT(DISTINCT FVL42.`value`) AS 'цвет металла',
GROUP_CONCAT(DISTINCT FP42.`id_feature_value`) AS metal,
FP9.`id_feature_value`, FVL9.`value` AS 'материал',
GROUP_CONCAT(DISTINCT FP8.`id_feature_value` ORDER BY FP8.`id_feature_value` ASC) AS features,
GROUP_CONCAT(DISTINCT FVL8.`value` ORDER BY FP8.`id_feature_value` ASC) AS 'тип украшения',
COUNT(DISTINCT FP8.`id_feature_value`) AS 'к-во типов',
GROUP_CONCAT(DISTINCT FP10.`id_feature_value` ORDER BY FP10.`id_feature_value` ASC) AS features,
GROUP_CONCAT(DISTINCT FVL10.`value` ORDER BY FP10.`id_feature_value` ASC ) AS 'вставки',
COUNT(DISTINCT FP10.`id_feature_value`) AS 'к-во вставок',
GROUP_CONCAT(DISTINCT PCP.`id_category`) AS catlist, COUNT(PCP.`id_category`) AS 'к-во категорий'
FROM `ps_category_product` AS PCP
LEFT JOIN `ps_product_lang` AS PPL
ON (PCP.`id_product`=PPL.`id_product`)
LEFT JOIN `ps_product` AS PP
ON (PCP.`id_product`=PP.`id_product`)
LEFT JOIN `ps_feature_product` AS FP8
ON (PPL.`id_product`=FP8.`id_product` AND FP8.`id_feature`=8)
LEFT JOIN `ps_feature_value_lang` AS FVL8
ON (FP8.`id_feature_value`=FVL8.`id_feature_value` )
LEFT JOIN `ps_feature_product` AS FP9
ON ( PPL.`id_product` = FP9.`id_product` AND FP9.`id_feature`=9)
LEFT JOIN `ps_feature_value_lang` AS FVL9
ON ( FP9.`id_feature_value` = FVL9.`id_feature_value` )
LEFT JOIN `ps_feature_product` AS FP10
ON ( PPL.`id_product` = FP10.`id_product` AND FP10.`id_feature`=10)
LEFT JOIN `ps_feature_value_lang` AS FVL10
ON ( FP10.`id_feature_value` = FVL10.`id_feature_value` )
LEFT JOIN `ps_feature_product` AS FP42
ON ( PPL.`id_product` = FP42.`id_product` AND FP42.`id_feature`=42)
LEFT JOIN `ps_feature_value_lang` AS FVL42
ON ( FP42.`id_feature_value` = FVL42.`id_feature_value` )
WHERE PCP.`id_product` IN ( SELECT PCP.`id_product` FROM `ps_category_product` AS PCP WHERE PCP.`id_category`=291)
GROUP BY PCP.`id_product`
Что не нравится:
В таблице `ps_feature_product` есть некоторые характеристики с кодами 8,9,10,42. Чтобы повыводить значения этих характеристик в отдельные столбцы, таблица `ps_feature_product` 4 раза присоединяется через LEFT JOIN.
Можно ли как-то сделать этот запрос короче и выбирая нужные характеристки с кодами 8,9,10,42 без использования многократного LEFT JOIN?
Спасибо
Отредактированно buldog52 (11.12.2016 22:28:35)
Неактивен
можно однократно и использовать конструкции с if:
GROUP_CONCAT(DISTINCT if(`id_feature`=42,`id_feature_value`,null)) AS metal,
Неактивен
vasya написал:
можно однократно и использовать конструкции с if:
GROUP_CONCAT(DISTINCT if(`id_feature`=42,`id_feature_value`,null)) AS metal,
Спасибо.
Запрос (работает) стал таким:
SELECT
PCP.`id_product` , PPL.`name` , PP.`reference` AS 'артикул',
GROUP_CONCAT(DISTINCT iF(PFP.`id_feature`=42,PFP.`id_feature_value`,null)) AS color,
GROUP_CONCAT(DISTINCT iF(PFP.`id_feature`=42,PFVL.`value`,null)) AS 'цвет металла',
GROUP_CONCAT(DISTINCT iF(PFP.`id_feature`=9,PFP.`id_feature_value`,null)) AS material,
GROUP_CONCAT(DISTINCT iF(PFP.`id_feature`=9,PFVL.`value`,null)) AS 'материал',
GROUP_CONCAT(DISTINCT iF(PFP.`id_feature`=8,PFP.`id_feature_value`,null)) AS type,
GROUP_CONCAT(DISTINCT iF(PFP.`id_feature`=8,PFVL.`value`,null)) AS 'тип украшения',
GROUP_CONCAT(DISTINCT iF(PFP.`id_feature`=10,PFP.`id_feature_value`,null)) AS incuts,
GROUP_CONCAT(DISTINCT iF(PFP.`id_feature`=10,PFVL.`value`,null)) AS 'вставки',
GROUP_CONCAT(DISTINCT PCP.`id_category`) AS catlist, COUNT(PCP.`id_category`) AS 'к-во категорий'
FROM `ps_category_product` AS PCP,
`ps_product_lang` AS PPL,
`ps_feature_product` AS PFP,
`ps_feature_value_lang` AS PFVL,
`ps_product` AS PP
WHERE PCP.`id_product` = PPL.`id_product`
AND PCP.`id_product` = PFP.`id_product`
AND PCP.`id_product`=PP.`id_product`
AND PFP.`id_feature_value` = PFVL.`id_feature_value`
AND PCP.`id_product` IN ( SELECT PCP.`id_product` FROM `ps_category_product` AS PCP WHERE PCP.`id_category`=291)
GROUP BY PCP.`id_product`
Отредактированно buldog52 (13.12.2016 01:11:51)
Неактивен