Perfidus Posted July 3, 2009 Share Posted July 3, 2009 Hello everyone! I'm using this query: SELECT * FROM $table WHERE ref='$ref' ORDER BY DATE(STR_TO_DATE( `thedate`, '%d/%m/%Y %H:%i:%s' )) ASC LIMIT $startv, $amount"; But all the time the results appear in the order given automatically by the primary key which is an autoincrement field not really shorted by date... Any hints? Quote Link to comment https://forums.phpfreaks.com/topic/164694-solved-order-by-date-doesnt-seem-to-work/ Share on other sites More sharing options...
PFMaBiSmAd Posted July 3, 2009 Share Posted July 3, 2009 You would need to show what the data is and what the results actually are. My guess is the format string '%d/%m/%Y %H:%i:%s' does not match the actual data (and this is another reason why you should use a DATETIME data type to begin with.) Quote Link to comment https://forums.phpfreaks.com/topic/164694-solved-order-by-date-doesnt-seem-to-work/#findComment-868507 Share on other sites More sharing options...
fenway Posted July 3, 2009 Share Posted July 3, 2009 Yes... show STR_TO_DATE() output first to verify. Quote Link to comment https://forums.phpfreaks.com/topic/164694-solved-order-by-date-doesnt-seem-to-work/#findComment-868514 Share on other sites More sharing options...
Perfidus Posted July 4, 2009 Author Share Posted July 4, 2009 The data on the cell and cell itself are datetime: 2009-06-14 20:34:20 Quote Link to comment https://forums.phpfreaks.com/topic/164694-solved-order-by-date-doesnt-seem-to-work/#findComment-868745 Share on other sites More sharing options...
Perfidus Posted July 4, 2009 Author Share Posted July 4, 2009 I will try to express myself better: My table has 3 rows: data_id: autoincrement, primary description: varchar thedate: datetime This is an example of content: data_id | description | thedate ------------------------------------ 1 | whatever info | 2009-06-14 20:34:20 2 | whatever info | 2009-06-12 20:34:20 3 | whatever info | 2009-06-18 20:34:20 I want to make a query that returns results from the most recent date to the older one so I'm using: SELECT * FROM $table WHERE ref='$ref' ORDER BY DATE(STR_TO_DATE( `thedate`, '%d/%m/%Y %H:%i:%s' )) ASC LIMIT $startv, $amount"; But all I get is the results in the order in which they where stored, not shorted by date... Does something like this exists? SELECT * FROM $table WHERE ref='$ref' ORDER BY DATETIME(STR_TO_DATETIME( `thedate`, '%d/%m/%Y %H:%i:%s' )) ASC LIMIT $startv, $amount"; If I try it I get an error, I guess it doesn't exist... Any hints? Quote Link to comment https://forums.phpfreaks.com/topic/164694-solved-order-by-date-doesnt-seem-to-work/#findComment-868749 Share on other sites More sharing options...
Perfidus Posted July 4, 2009 Author Share Posted July 4, 2009 And this can be the solution: SELECT * FROM $table WHERE ref='$ref' ORDER BY DATE_FORMAT( `thedate`, '%d/%m/%Y %H:%i:%s' ) ASC LIMIT $startv, $amount"; At least it seems to work !! Quote Link to comment https://forums.phpfreaks.com/topic/164694-solved-order-by-date-doesnt-seem-to-work/#findComment-868754 Share on other sites More sharing options...
PFMaBiSmAd Posted July 4, 2009 Share Posted July 4, 2009 The column thedate is a DATETIME value, you use it directly in the ORDER BY. If you want the most recent date to the older, use DESC - SELECT * FROM $table WHERE ref='$ref' ORDER BY thedate DESC LIMIT $startv, $amount"; Quote Link to comment https://forums.phpfreaks.com/topic/164694-solved-order-by-date-doesnt-seem-to-work/#findComment-868773 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.