0

my mysql query dont humanize the ORDER BY.

SELECT COUNT(level) as count, level
from logtest
GROUP BY level
ORDER BY level;

Sample:

6
5
5
5
13
0

The correct would be

13
6
5
5
5
0

any help?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Jam
  • 109
  • 3
  • 10
  • 2
    please share you MySql query – Sid M May 02 '17 at 14:49
  • SELECT COUNT(level) as count, level from logtest GROUP BY level ORDER BY level; – Jam May 02 '17 at 14:54
  • 2
    you mean `order by cast(level as unsigned) desc`? The issue appears to be your data is text when you need it to be numeric to sort correctly. so... http://stackoverflow.com/questions/12126991/cast-from-varchar-to-int-mysql – xQbert May 02 '17 at 14:56
  • 1
    if you want your data to be sorted as a string, than you have to store it as a string, not a number – Your Common Sense May 02 '17 at 14:56
  • all is like string in this table. thank you guys. "cast" work for me. I will save this command for later use too! thank you folks! – Jam May 02 '17 at 15:03

1 Answers1

1
SELECT COUNT(level) as count, level
FROM logtest
GROUP BY level
ORDER BY cast(level as unsigned);

Near Dup of: Cast from VARCHAR to INT - MySQL

Not quite a dup because the issues on sort is that it's sorting by a text field (my guess is level is of character type) when you want it to sort by numeric type... so just cast which the above link describes how.

Community
  • 1
  • 1
xQbert
  • 34,733
  • 2
  • 41
  • 62