【发布时间】:2014-06-04 15:51:14
【问题描述】:
我有一个包含以下字段的表:
+------------------------------+
| id | cart1 | cart2 | cart3 |
|------------------------------|
| 1 | ball | soap | NULL |
| 2 | apple | towel | paper |
| 3 | soap | ball | NULL |
| .... | ..... | ..... | ..... |
+------------------------------+
我想要以下输出:
+-----------------------------------------+
| item1 | item2 | item3 | num_appearances |
|-----------------------------------------|
| ball | soap | NULL | 2 |
| apple | towel | paper | 1 |
| ..... | ..... | ..... | ............... |
+-----------------------------------------+
基本上cart1、cart2和cart3定义了一个人的购物车,但是顺序无所谓,我想统计一组物品一起购买的次数,再次与顺序无所谓。所以apple、towel、paper在样本表中出现了一次,ball和soap出现了两次。
我认为我需要做的是对item1、item2 和item3 进行排序,将它们连接起来,然后按连接后的值进行分组。所以group_concat 听起来很棒,我可以按id 分组,或者每行不同的其他列。但到目前为止我有group_concat(item1, item2, item3 [ORDER BY WHAT])。但是如何对列列表进行排序并返回该连接的排序列表?
【问题讨论】:
-
我首先将三个
cart#列合并为一个列。 -
@TrippKinetics - 这就是我想要通过
group_concat实现的目标。很遗憾,我无法更改架构。
标签: mysql sorting group-by concatenation group-concat