1

I have been using MySQL for a long time and I have never run across this issue. I have a table that stores the scores for an application. For some reason, when I sort by score ASC, the highest score is shown first with the lowest score being last. Please see the screenshot below:

enter image description here

Here is my query:

SELECT category, subject, max(score) as score FROM scores 
WHERE customer_id = 1086 AND category = 'Business' 
GROUP BY subject ORDER BY score ASC

Any thoughts on why this is happening?

4
  • 1
    This may be happening as the Score is being treated as a string instead of a number. Commented Jan 30, 2014 at 5:01
  • Is there a way to change this? Commented Jan 30, 2014 at 5:01
  • can you dump your table structure here?some time incorrect data type gone crazy while sorting. Commented Jan 30, 2014 at 5:02
  • 1
    Try to cast your score before sorting in SQL and see what happens. Commented Jan 30, 2014 at 5:03

2 Answers 2

4

Change the datatype of score from string (eg varchar/text) to a number (eg int). That should solve the sorting issue.

When the values are sorted on string basis (alphabetically), the '6' in '60' comes before '8'.

As a temporary work-around you can also try order by score+0 asc to try and convert your value into a number.

Sign up to request clarification or add additional context in comments.

Comments

0

Try selecting CAST(max(score) AS INTEGER) as Score and then ORDER BY Score.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.