How to sort date from past to present

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
rei27
Forum Commoner
Posts: 27
Joined: Thu Apr 30, 2009 4:17 am

How to sort date from past to present

Post by rei27 »

I wishes my data will show according to the date, but no matter how i try, it cannot be!

I am using this code:

Code: Select all

SELECT * FROM customer ORDER BY date DESC
The result shows as:
1 Jacky 30.11.2009
2 Willy 02.12.2009
3 Jason 01.12.2009
Actually I wishes my result show as:
1 Willy 02.12.2009
2 Jason 01.12.2009
3 Jacky 30.11.2009
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: How to sort date from past to present

Post by John Cartwright »

You need to either change your date format, or better yet use the DATE field (which comes along with many great date functions). The reason why you are getting unexpected results if because these are being treated as strings. To mysql, it simply looks like

30112009
02122009
01122009

So yes, they are being ordered correctly against your query. You would need to format your dates by YYYY.MM.DD for this to work without the DATE field.
Post Reply