简体   繁体   English

自定义MYSQL计数结果的排序顺序?

[英]Custom Sort Order for MYSQL Count Results?

I am trying to write a report on some data I collected using a google form. 我正在尝试编写有关我使用Google表单收集的一些数据的报告。 Each person was asked how many of an item they had in their closet. 每个人都被问到他们衣柜里有多少东西。 I want to present the data as a count of how many out of the total fell into each range. 我想将数据表示为落入每个范围的总数中的多少。 So, I used this mysql query to count the instances of each answer: 因此,我使用了这个mysql查询来计算每个答案的实例:

SELECT  `Closet` , COUNT( * ) FROM  `TABLE 1` GROUP BY  `Closet`

And here is the resulting data: 这是结果数据:

Closet  | COUNT( * )
--------+------------
0       | 8
1-5     | 124
101-200 | 7
11-20   | 181
201-300 | 3
21-50   | 171
51-100  | 48
6-10    | 156

The problem is that alphabetically, 101-200 items sorts before 6-10 items. 问题在于,按字母顺序,101-200个项目在6-10个项目之前排序。 I basically want to sort this in some way that would put the number ranges in a logical order. 我基本上想以某种方式对这个排序,以使数字范围符合逻辑顺序。 (1-5, 6-10, 11-20, etc). (1-5、6-10、11-20等)。

How can I accomplish this? 我该怎么做?

You'll have to use convert & substring_index . 您必须使用convertsubstring_index

SELECT `Closet` , COUNT( * ) FROM `TABLE 1` GROUP BY `Closet` order by convert(substring_index(Closet,'-',1), unsigned integer) 

This will sort the Closet by taking the first number of the ranges, which should essentially do the job. 这将通过选择范围的第一个数字对壁橱进行排序,这实际上应该可以完成工作。

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

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