Answer the question
In order to leave comments, you need to log in
How to merge attributes and values in EAV data model?
Let me explain. There are three tables products, attributes and values. I'm trying to get a product with its attribute and value with such a request.
SELECT
`prod`.`name`,
`prod`.`price`,
GROUP_CONCAT(`atr`.`nameAttribute`) as attributeName,
GROUP_CONCAT(`val`.`attributeValue`) as attributeValue
FROM `products` as `prod`
LEFT JOIN `productAttribute` as `atr` ON `prod`.`idProduct` = `atr`.`idProduct`
LEFT JOIN `attributeValue` as `val` ON `atr`.`idAttribute` = `val`.`idAttribute`
WHERE `prod`.`idCategory` = :idCategory
GROUP BY prod.idProduct;
Answer the question
In order to leave comments, you need to log in
SELECT `prod`.`name`, `prod`.`price`,
GROUP_CONCAT(CONCAT(`atr`.`nameAttribute`, '=' , `val`.`attributeValue`) as `attribute`,
FROM `products` as `prod`
LEFT JOIN `productAttribute` as `atr` ON `prod`.`idProduct` = `atr`.`idProduct`
LEFT JOIN `attributeValue` as `val` ON `atr`.`idAttribute` = `val`.`idAttribute`
WHERE `prod`.`idCategory` = :idCategory
GROUP BY `prod`.`idProduct`;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question