[SOLVED] Ordering problem

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
sk8erh4x0r
Forum Commoner
Posts: 43
Joined: Wed May 26, 2004 8:27 pm

[SOLVED] Ordering problem

Post by sk8erh4x0r »

I'm using this query:

SELECT * FROM `tutorials` ORDER BY `chars` DESC

the chars field holds a number value

The problem i'm having is that it's not ordering the way it should because there are high values below the lower values like so:

5435
2456
11254

Why isn't 11254 at the top?
Last edited by sk8erh4x0r on Thu Aug 11, 2005 4:42 pm, edited 2 times in total.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

What kind of field is chars?
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

what type is "chars" it looks like its a character string, so its sorting "alphabetically" not numerically. You might be able to cast it to an integer to make it sort properly. In PostGreSQL I'd try

Code: Select all

SELECT * FROM tutorials ORDER BY chars::INT DESC;
but I don't know the syntax fo MySQL.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

MySQL ghetto quickie:

Code: Select all

SELECT * FROM tutorials ORDER BY chars+0 DESC;
Post Reply