Jump to content

ORDER BY DAY,MONTH,YEAR


datoshway

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.