mysql group by multiple columns with count - 不对所有答案进行分组

时间:2015-10-12 16:26:51

标签: mysql count group-by

我正在按两列进行分组,并试图弄清楚为什么两列精确的列没有相加。我已经删除了所有其他数据,并且我试图使单个集合正常工作,理论上它将使其他集合也能正常工作。以下是我得到的示例输出:

Question | Answer      | Count
Waiver   | Waiver Info |  11
Waiver   | Waiver Info |   7

预期产出:

Question | Answer      | Count
Waiver   | Waiver Info |  18

查询是:

SELECT question, answer, count(*) as `count` FROM orderitems oi
JOIN orders o ON o.orderid = oi.oid 
JOIN ordermeta om ON om.groupid = oi.groupid
WHERE question = 'waiver:'
group by question, answer
order by question, answer

将会有其他问题和答案被分成更多信息与计数,所以我必须按列分组并按列排序.​​..问题和答案完全相同,所以我可以'弄清楚他们为什么不将这两行加在一起。

1 个答案:

答案 0 :(得分:0)

您可能在字段中有空格,这就是为什么它会向您显示两行而不是单个结果。在分组之前,您可以用空格字符串替换空格。

以下是这样做的方法:

SELECT question, answer, count(*) as `count` FROM orderitems oi
JOIN orders o ON o.orderid = oi.oid 
JOIN ordermeta om ON om.groupid = oi.groupid
WHERE question = 'waiver:'
group by replace(question,' ',''), replace(answer,' ','')
order by question, answer