MySQL Group By寻找独特的组合

时间:2017-02-01 15:00:22

标签: mysql sql count group-by sum

首先为模糊的标题道歉,我真的不知道如何表达它,我认为这就是为什么我还没有找到解决方案。

使用经过修改的OSCommerce安装,我试图在表格中查找自定义报告的已排序产品选项/属性的唯一组合。产品的每个选项/属性都存储在一行中。产品可以(并且确实)具有多个选项/属性。

我有以下SQL查询

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并不重要):

+---------+-----------------+
| attrCnt | prodOptionCombo |
+---------+-----------------+
|       1 | L - Blue        |
|       2 | M - Blue        |
|       3 | S - Blue        |
|       1 | S - Yellow      |
+---------+-----------------+

包含此信息的表是store_orders_products_attributes:

products_options列可以包含任何分类文本分组(自由文本),例如Select Flavor,Select xyz等,因此它不会始终为Color / Size

每个产品并不总是两个选项,可能是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                       |
    +-------------------------------+-----------+--------------------+------------------+-------------------------+

store_orders_products表包含:

+--------------------+-----------+-------------+----------------+---------------+-------------------+
| 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 |
+--------------------+-----------+-------------+----------------+---------------+-------------------+

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

要开始使用,请使用此子查询而不是原始表:

(
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