MySQL分组以查找唯一的组合
首先,我为这个模糊的标题道歉,我真的不知道如何表达它,我想这就是为什么我还没有找到解决办法的原因 使用修改后的OSCommerce安装,我试图在自定义报告的表中找到订购产品选项/属性的唯一组合。产品的每个选项/属性都存储在一行中。一个产品可以(而且确实)有多个选项/属性 我有以下SQL查询MySQL分组以查找唯一的组合,mysql,sql,count,group-by,sum,Mysql,Sql,Count,Group By,Sum,首先,我为这个模糊的标题道歉,我真的不知道如何表达它,我想这就是为什么我还没有找到解决办法的原因 使用修改后的OSCommerce安装,我试图在自定义报告的表中找到订购产品选项/属性的唯一组合。产品的每个选项/属性都存储在一行中。一个产品可以(而且确实)有多个选项/属性 我有以下SQL查询 SELECT sum(op.products_quantity) AS attrCnt, opa.products_options AS prodOption, opa.products_options_va
SELECT
sum(op.products_quantity) AS attrCnt,
opa.products_options AS prodOption,
opa.products_options_values AS prodOptionValue
FROM
store_orders_products_attributes opa
LEFT JOIN store_orders o ON o.orders_id = opa.orders_id
LEFT JOIN store_orders_products op ON op.orders_products_id = opa.orders_products_id
WHERE
o.customers_id = '99999'
AND (o.date_purchased BETWEEN CAST('2017-01-03' AS DATE)
AND CAST('2017-02-01' AS DATE))
AND op.products_id = 88888
GROUP BY
opa.products_options_values
ORDER BY
opa.products_options,
opa.products_options_values
返回:
+---------+---------------+-----------------+
| attrCnt | prodOption | prodOptionValue |
+---------+---------------+-----------------+
| 6 | Select Colour | Blue |
| 1 | Select Colour | Yellow |
| 1 | Select Size | L |
| 2 | Select Size | M |
| 4 | Select Size | S |
+---------+---------------+-----------------+
在此期间订购的产品总数为7件,1件大的,2件中的,4件小的,-6件蓝的,1件黄的。从这一点我无法判断L(大)是蓝色还是黄色等
我想要的结果如下(无论是L-蓝色还是蓝色-L都不重要):
包含此信息的表是存储\订单\产品\属性:
“产品选项”列可以包含任何分类文本分组(自由文本),例如“选择味道”、“选择xyz”等,因此它不总是颜色/大小
每个产品也不总是有两个选项,可以是0、1、12或更多
+-------------------------------+-----------+--------------------+------------------+-------------------------+
| orders_products_attributes_id | orders_id | orders_products_id | products_options | products_options_values |
+-------------------------------+-----------+--------------------+------------------+-------------------------+
| 1420 | 596 | 2626 | Select Colour | Blue |
| 1421 | 596 | 2626 | Select Size | M |
| 1438 | 600 | 2656 | Select Colour | Blue |
| 1439 | 600 | 2656 | Select Size | M |
| 1445 | 601 | 2668 | Select Colour | Blue |
| 1446 | 601 | 2668 | Select Size | S |
| 1447 | 602 | 2671 | Select Colour | Yellow |
| 1448 | 602 | 2671 | Select Size | S |
| 1464 | 611 | 2705 | Select Colour | Blue |
| 1465 | 611 | 2705 | Select Size | S |
| 1502 | 634 | 2791 | Select Colour | Blue |
| 1503 | 634 | 2791 | Select Size | L |
+-------------------------------+-----------+--------------------+------------------+-------------------------+
门店\订单\产品表包含:
+--------------------+-----------+-------------+----------------+---------------+-------------------+
| orders_products_id | orders_id | products_id | products_model | products_name | products_quantity |
+--------------------+-----------+-------------+----------------+---------------+-------------------+
| 2626 | 596 | 88888 | Code123 | Gloves | 1 |
| 2656 | 600 | 88888 | Code123 | Gloves | 1 |
| 2668 | 601 | 88888 | Code123 | Gloves | 1 |
| 2671 | 602 | 88888 | Code123 | Gloves | 1 |
| 2705 | 611 | 88888 | Code123 | Gloves | 2 |
| 2791 | 634 | 88888 | Code123 | Gloves | 1 |
+--------------------+-----------+-------------+----------------+---------------+-------------------+
有什么想法吗 要开始,请使用此子查询而不是原始表:
(
select a1.orders_id,
a1.products_options_values as sizeval,
a2.products_options_values as colourval
from store_orders_products_attributes a1
inner join store_orders_products_attributes a2
on a1.orders_id = a2.orders_id
where a1.products_options = 'Size'
and a2.products_options = 'Colour' -- Yay, another brit
) x1
我不明白为什么他们在同一列(产品选项值)中存储两个不同的属性(颜色和大小),这不是一个概念问题吗?@mounaim你必须和OSCommerce的原始创建者Harald Ponce de Leon谈谈。在订购产品之前,信息以类似的方式存储,因此他们在订购后以类似的方式保存信息。选项可以有不同的定价/重量+/-money kg等。订单显示为产品名称--选择颜色=蓝色--选择大小=L,以这种方式存储数据。@mounaim还可以选择数量不受限制的选项,如果大小、颜色、xyz等有自己的列,则可能有100个列great idea,然而,很抱歉,我应该在最初的帖子中说,产品选项可以包含客户输入的任何分类、选择风味、选择容量等
(
select a1.orders_id,
a1.products_options_values as sizeval,
a2.products_options_values as colourval
from store_orders_products_attributes a1
inner join store_orders_products_attributes a2
on a1.orders_id = a2.orders_id
where a1.products_options = 'Size'
and a2.products_options = 'Colour' -- Yay, another brit
) x1