colourtheweb
colourtheweb

Reputation: 777

How should I fetch data in descending order from database according to the date_time?

Problem I have the last data entry in date_time for 9th of april 2013 whereas when I try to fetch in descending order from the database it is giving me 8th April 2013. Please see the image and Code below. Any help will be appriciated.

Database enter image description here

Code

SELECT * 
FROM data_feeds
WHERE username =  '[email protected]'
AND gadget_data_type =  'Weighin'
ORDER BY STR_TO_DATE( date_time,  '%D, %j %M %Y %H:%i:%s' ) DESC 
LIMIT 1

Upvotes: 0

Views: 1469

Answers (2)

nl-x
nl-x

Reputation: 11832

Your format for str_to_date() is wrong You say %D, %j %M %Y %H:%i:%s So according to you the second value is %j. But according to Mysql docs %j is 'day of year'. https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

edit: you have multiple errors in the format. Try: %a, %e %b %Y %H:%i:%s

Upvotes: 0

James
James

Reputation: 22246

Format string is broken, try: '%a, %e %b %Y %H:%i:%s'

Upvotes: 1

Related Questions