简体   繁体   English

聚合MySQL数据,正在使用分组依据,但是它并没有完全满足我的要求

[英]Aggregate MySQL data, am using group by, but it isn't quite doing what I want

I have a community driven database for an iOS game. 我有一个社区驱动的iOS游戏数据库。 I am trying to aggregate the data I've collected by the community, and it looks something like this: 我正在尝试汇总社区收集的数据,看起来像这样:

+--------+--------+--------------+
| color1 | color2 | result_color |
+--------+--------+--------------+
| red    | blue   | purple       |
| blue   | red    | purple       |
| red    | blue   | purple       |
| yellow | blue   | green        |
+--------+--------+--------------+

Currently, I am running the following query: 当前,我正在运行以下查询:

select
    count(*) as count,
    `Mixes`.*
from
    `mixes` AS `Mixes`
where
    `result_color` = 'purple'
group_by
    color1,
    color2
order by
    `count` desc

Which produces the following output: 产生以下输出:

+-------+--------+--------+--------------+
| count | color1 | color2 | result_color |
+-------+--------+--------+--------------+
| 2     | red    | blue   | purple       |
| 1     | blue   | red    | purple       |
+-------+--------+--------+--------------+

However, I would like it to produce the following output, since when mixing colors it doesn't matter which color you mix first: 但是,我希望它产生以下输出,因为混合颜色时,首先混合哪种颜色并不重要:

+-------+--------+--------+--------------+
| count | color1 | color2 | result_color |
+-------+--------+--------+--------------+
| 3     | red    | blue   | purple       |
+-------+--------+--------+--------------+

So, my question is, how can I aggregate data over 2 columns such that when color1 is red and color2 is blue, that the aggregate function treats it the same as when color1 is blue and color2 is red? 因此,我的问题是,如何在2列上聚合数据,以便当color1为红色而color2为蓝色时,聚合函数将其与color1为蓝色且color2为红色时相同?

Thanks in advance! 提前致谢!

If you just have the two color columns you can simply order them with a CASE expression: 如果只有两个颜色列,则只需使用CASE表达式对它们进行排序:

SELECT
  COUNT(*) AS `count`,
  CASE WHEN color1 < color2 THEN color1 ELSE color2 END AS color1,
  CASE WHEN color1 >= color2 THEN color1 ELSE color2 END AS color2,
  resultcolor
FROM mixes
WHERE resultcolor = 'purple'
GROUP BY
  CASE WHEN color1 < color2 THEN color1 ELSE color2 END,
  CASE WHEN color1 >= color2 THEN color2 ELSE color1 END,
  resultcolor
ORDER BY `count`;

here is the logic that I have in mind and you need to implement it.. 这是我要记住的逻辑,您需要实现它。

You basically have a inner select query that selects the required final select columns, plus a new custom column called color, which is basically a concatenation of color1 and color2 but with the added logic that the lexicographically smaller string is concatenated with the larger string always which can be done using a switch/if statement in SQL. 你基本上有一个选择需要的最终选择列内选择查询,加上被称为颜色的新的自定义列,它基本上是一个串联color1color2的字典序小串总是结合一个较大的字符串添加的逻辑,但与可以使用SQL中的switch / if语句完成。

thus your custom color column will look like bluered for both the rows. 因此您的自定义color列对于两行都将看起来像是bluered

and now you have an outer query that groups by this custom column and you should get your desired output. 现在您有了一个外部查询,可以按此自定义列进行分组,并且应该获得所需的输出。

The SQL for the comparison and select should be as follows: 用于比较和选择的SQL应该如下所示:

SELECT CASE WHEN STRCMP(color1,color2)<=0 THEN color2+''+color1 ELSE color1+''+color2 END
FROM Mixes
WHERE result_color='purple'

Hope that works. 希望能奏效。 I might try and write the entire SQL query if I am feeling good about myself.. 如果我对自己感觉很好,我可能会尝试编写整个SQL查询。

Also I did not understand the reason for grouping it by the two colors when you can just group it by the result_color but I tried to answer your question as you have asked (may be you have your reasons) 另外,当您仅可以按result_color分组时,我也不明白将其按两种颜色分组的原因,但是我尝试按照您的要求回答您的问题(可能是您有原因)

Try this 尝试这个

SELECT COUNT(*) AS `Count`, M.*
FROM
(SELECT
  CASE WHEN color1 < color2 THEN color1 ELSE color2 END AS color1,
  CASE WHEN color1 > color2 THEN color1 ELSE color2 END AS color2,
  result_color
FROM mixes
WHERE result_color = 'purple') M
GROUP BY M.color1, M.color2
ORDER BY
    `Count` DESC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM