datoshway Posted March 9, 2011 Share Posted March 9, 2011 I am having to build on a database structure I did not develop, we have 3 fields that set the date day, month, year. How can I set up a query to order these chronological? I tried this but does not work. $sql = "SELECT * FROM calendar_event GROUP BY day, month, year ORDER BY month ASC"; Link to comment https://forums.phpfreaks.com/topic/230059-order-by-daymonthyear/ Share on other sites More sharing options...
Pikachu2000 Posted March 9, 2011 Share Posted March 9, 2011 Can you add a field to the table, and insert the date properly, or is it far too late for that? Link to comment https://forums.phpfreaks.com/topic/230059-order-by-daymonthyear/#findComment-1184856 Share on other sites More sharing options...
trinaryoc Posted March 9, 2011 Share Posted March 9, 2011 Try this out: SELECT CAST(CONCAT_WS("-", year, day, month) AS DATE) as 'ymd' FROM calendar_event GROUP BY ymd ORDER BY ymd ASC will give you a date output as a data type DATE and that's easy enough to ORDER BY. there's other ways to do it but that's a quick and dirty one. persionaly if you cant adjust the table, I'd create a VIEW and with the CASTed CONCAT instead of the year, month, day, so you dont have to run the CAST and CONCAT everytime time you need to sort by dates Link to comment https://forums.phpfreaks.com/topic/230059-order-by-daymonthyear/#findComment-1184905 Share on other sites More sharing options...
datoshway Posted March 9, 2011 Author Share Posted March 9, 2011 That didn't work, got the following error when trying to use that query Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in..... Link to comment https://forums.phpfreaks.com/topic/230059-order-by-daymonthyear/#findComment-1184994 Share on other sites More sharing options...
PFMaBiSmAd Posted March 9, 2011 Share Posted March 9, 2011 That is a php error and it generally means that your query failed for some reason, but it could also mean you are using the wrong variable name in your php statements or you are overwriting the php variable that did contain the result resource from the query. What's your actual php code from the line where you are forming the query in the $sql variable through to the line where the mysql_num_rows() statement is at? Link to comment https://forums.phpfreaks.com/topic/230059-order-by-daymonthyear/#findComment-1184996 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.