php forum
php mysql forum
php mysql smarty
 
Topic Options
#312648 - 01/12/07 10:20 AM MySQL query sorting
Myke Offline
Power User

Registered: 09/06/99
Posts: 87
Loc: Sydney, Australia

   MySQL query sorting to Del.icio.us Add to del.icio.us
  Digg MySQL query sorting Digg it
I've got a field in a table of type VARCHAR(8). The various values contained in this field include positive and negative numbers, and plain text.

When I run a query, sorting on this field, it doesn't appear to sort correctly. In my ORDER BY I'm type casting this field as follows:

ORDER BY cast(MyField as signed)

Now, if the following values are found in this field all throughout my table:

6, 3, down, slam, stgr, 0, -3

and the query is run, I get something like:

6
3
down
slam
stgr
down
0
-3

I have no idea why this sorts weirdly. I mean, even multiple instances of the same text values (like "down") aren't even appearing together.

The only thing that I find suspicious is that the Collation for this field (and all fields actually) is latin1_swedish_ci, but when I tried changing it to something like utf8_general_ci, it makes no difference.

To see this live, check out: http://virtuafighter.com/commands/index.php?chara=akira&ver=5b&order=mc&sort=desc

It's basically a command list for a video game. Clicking on the column headings sorts on that column. If you click on the "MC" column, you'll see exactly what I mean with the weird results. Sorting on the "Dmg" column seems fine, however. This field is of the same type, but does not include any text values.

Any clues on how I can sort a field with a mixture of positive and negative numbers, and text, would be appreciated.

Top
#312652 - 01/12/07 08:18 PM Re: MySQL query sorting [Re: Myke]
Ian Spence Offline
Master Hacker

Registered: 01/25/03
Posts: 3765
Loc: Saint Johns, PA
when you cast strings to integers, they are cast to 0 if they aren't numeric.

So your actual results with sorting were

6
3
0
0
0
0
0
-3


Stop casting them to integers and you should be good to go
_________________________
Code monkey like Fritos

Top
#312655 - 01/12/07 09:43 PM Re: MySQL query sorting [Re: Ian Spence]
Myke Offline
Power User

Registered: 09/06/99
Posts: 87
Loc: Sydney, Australia
Thanks for the explanation.

If I don't cast, then, as an example, the number values will sort like this:

1
10
11
2
20
22

instead of:

1
2
10
11
20
22

Is there a way to correctly sort a VARCHAR field containing positive numbers, negative numbers and plain text?


Edited by Myke (01/12/07 09:46 PM)

Top
#312659 - 01/13/07 12:50 AM Re: MySQL query sorting [Re: Myke]
Myke Offline
Power User

Registered: 09/06/99
Posts: 87
Loc: Sydney, Australia
Found a solution to work.

I ended up using a CASE statement in my SELECT query to assign numerical values to my strings. It basically goes:

SELECT * FROM MyTable
ORDER BY
CASE MyField
WHEN 'string1' THEN 100
WHEN 'string2' THEN 200
and so on...
ELSE cast(MyField as signed)
END

Cheers.

Top


Top Posters Last 30 Days
AllenAyres 18
Gizmo 12
sirdude 6
GEN 3
tackaberry 2
Philipp 2
willing 2
Who's Online
0 Registered (), 35 Guests and 11 Spiders online.
Key: Admin, Global Mod, Mod
Shout Box

Latest Posts
Team UBBDev Rides Again!
by Gizmo
08/28/08 11:45 PM
Multiple Identity Detector
by MattUK
08/28/08 04:10 PM
[7.3.x] ubb.links
by AllenAyres
08/26/08 09:57 AM
Installing FlashChat with 7.3
by Gizmo
08/23/08 05:36 AM
[7.2.1] - Naked shoutbox
by Iann128
08/22/08 07:27 PM
Nice Ajax Chat
by M4D
08/21/08 10:02 PM
51-card NVIDIA folding rig cranks out 265,200 ppd
by AllenAyres
08/13/08 10:10 PM
New Mods
Installing FlashChat with 7.3
by Paug
08/23/08 12:14 AM
[7.x] AddThis - Social Networking
by Gizmo
08/01/08 01:30 AM
[7.3.x] ubb.links
by AllenAyres
06/20/08 11:50 PM
[7.3] Dissallow quotes more then 3 levels deep
by blaaskaak
06/09/08 09:01 AM
[7.2.1] - Naked shoutbox
by sirdude
08/17/07 10:36 PM
Newest Members
welcomeback1, Paug, Tim Keating, anscers, Murphdog
13324 Registered Users

 

 

 
fusionbb message board php hacks