views:

79

answers:

5

The following query:

SELECT DISTINCT ClassName FROM SiteTree ORDER BY ClassName

is returning things in no apparent order!

I get the same result whether I quote column/table names, or use DISTINCT or not, or add ASC or DESC.

I assumed the indexes might be broken, or something like this, so tried dropping and recreating. Also tried REPAIR TABLE and CHECK TABLE.

The table collation is set to latin1_swedish_ci. All the textual columns are set to use UTF-8 and collation is set to utf8_general_ci

What could be causing this?

Edit:

Sample data at pastie.

Results are direct from SQL query executed in MySQL client (tried 2 different client apps).

A: 

It is possible, or indeed likely, that the table is sorted before DISTINCTing. Try using an aggregate function or GROUP BY

Williham Totland
even if the table was sorted before the distinct, it would still be sorted.
jishi
A: 

If you are looking at the results on a web page you may be just seeing a cached resultset from an earlier query.

mosheb
+1  A: 

There is no reason this should work, by see if getting rid of the mix of camel casing makes a difference:

  SELECT DISTINCT LOWER(LTRIM(ClassName)) AS classname 
  FROM SiteTree ORDER BY classname

I liked Cruachan's idea so much, I updated my out-there idea to cover it. Remove leading spaces.

Anthony
+3  A: 

How was your data loaded? I've seen a few occasions where loading from some external source has placed a whitespace or other similar character in the first position of a string - with the result that the returned result set is actually sorted, but not as one would expect.

It can be incredibly difficult to detect this sort of thing and if I get anomalous results of the type you're seeing on of the first things I tend to do is select the field in question concatenating '>' and '<'.

Cruachan
That would be the first thing I would check, do "SELECT char_length(ClassName), ClassName" and look for anomalities.
jishi
In this case there were no problems like that, but it's always a good thing to check.
Luke H
+2  A: 

Eureka!

While it's true that using a function would return the correct order, e.g:

  SELECT DISTINCT ClassName FROM SiteTree ORDER BY REPLACE(ClassName,'','')

It turns out that I was looking at an ENUM column (I'd forgotten, thought it was plain text), and so MySQL was sorting according to the order of the items in the ENUM.

Thanks for the helpful suggestions however.

Here's a reasonable solution, given the situation.

SELECT DISTINCT ClassName FROM SiteTree ORDER BY CAST(ClassName AS CHAR) 
Luke H
I normally don't +1 when a users realizes they just forgot something, but your answer explains a situation that others could easily find themselves in and gives the function to fix it. Glad you worked it out and posted to let us know.
Anthony
Cheers! I find a lot of helpful things here, hopefully I save someone else from one of those banging-head-against-desk moments.
Luke H