我需要一个SQL查询(MySQL数据库),它确定各种参数(属性)的产品数量。
我尝试这个sql查询 - 我模拟选择属性“Color black”
SELECT codmm.id_parameter_codebook, COUNT(codmm.id_parameter_codebook) count_products,
GROUP_CONCAT(DISTINCT prod.title ORDER BY prod.id) products_title
FROM catalog_parameter_product_value_parameter_codebook_mm codmm
JOIN catalog_parameter_product_value pv ON pv.id=codmm.id_parameter_product_value
JOIN catalog_parameter p ON p.id=pv.id_parameter
JOIN (
SELECT p.id, p.title
FROM catalog_product p
JOIN catalog_parameter_product_value pv ON p.id=pv.id_product
JOIN catalog_parameter_product_value_parameter_codebook_mm codmm ON pv.id=codmm.id_parameter_product_value
JOIN catalog_parameter par ON par.id=pv.id_parameter
WHERE (codmm.id_parameter_codebook = 19)
GROUP BY p.id
) prod ON prod.id=pv.id_product
GROUP BY codmm.id_parameter_codebook ORDER BY p.title
但是此查询的输出不包含属性(颜色为绿色),这是一个加1产品。
表格中填写的数据:
表“catalog_parameter”:
+----+-------+
| id | title |
+----+-------+
| 1 | Color |
| 3 | Width |
+----+-------+
表“catalog_parameter_product_value”:
+-----+------------+--------------+
| id | id_product | id_parameter |
+-----+------------+--------------+
| 260 | 11 | 1 |
| 261 | 11 | 3 |
| 262 | 9 | 1 |
| 263 | 9 | 3 |
| 264 | 8 | 1 |
| 265 | 8 | 3 |
+-----+------------+--------------+
表“catalog_product”
+----+-----------+
| id | title |
+----+-----------+
| 8 | Product 3 |
| 9 | Product 2 |
| 11 | Product 1 |
+----+-----------+
表“catalog_parameter_product_value_parameter_codebook_mm”:
+-----+----------------------------+-----------------------+
| id | id_parameter_product_value | id_parameter_codebook |
+-----+----------------------------+-----------------------+
| 139 | 260 | 19 |
| 140 | 261 | 20 |
| 141 | 262 | 19 |
| 142 | 263 | 21 |
| 143 | 264 | 24 |
| 144 | 265 | 20 |
+-----+----------------------------+-----------------------+
表“catalog_parameter_codebook”:
+----+--------------+-------+
| id | id_parameter | title |
+----+--------------+-------+
| 18 | 1 | white |
| 19 | 1 | black |
| 20 | 3 | 100 |
| 21 | 3 | 200 |
| 22 | 3 | 300 |
| 23 | 3 | 400 |
| 24 | 1 | green |
+----+--------------+-------+
选择属性后的示例:
如果您不选择任何参数(属性),输出如下所示:
- Color
*** black (2)
*** green (1)
- Width
*** 100 (2)
*** 200 (1)
如果选择属性“Color black”,我应该得到输出
- Color
*** black (2) - ACTIVE
*** green (+1)
- Width
*** 100 (1)
*** 200 (1)
如果选择属性“Color black”和“Width 100”,我应该得到输出
- Color
*** black (1) - ACTIVE
*** green (0)
- Width
*** 100 (+1)
*** 200 (1) - ACTIVE
如果你选择一个属性“Color black”一个“Color green”我应该得到输出
- Color
*** black (2) - ACTIVE
*** green (1) - ACTIVE
- Width
*** 100 (1)
*** 200 (1)
用于测试的SQL Fiddle url url:http://sqlfiddle.com/#!9/f4599/2