Jump to content

mysql query - help


ricmetal

Recommended Posts

hi dudes

how do i write a mysql query with 3 columns, where the first column is 'year', the second is 'month' (integer) and the third is 'day' (integer), ordered by desc, but with an extra quirk, where if any of the three columns is zero (which means there is no data for that date column - assume i have a year and a month, but no day)?

 

my code looks like the following

ORDER BY exhib_date_year DESC, exhib_date_month DESC, exhib_date_day DESC

Link to comment
Share on other sites

what appears on my website is not the data i fetch from these three date database columns (the columns are on the database!).

what i am trying to do is to display (on my website) the data fetched from other columns, and ordered by these three date columns, by descent, with the extra quick where if any of the three date columns is zero, those rows will be fetched first.

 

Link to comment
Share on other sites

the only thought I have is if you have the year, month, and day in different columns, you could make it so that if the number is 0, use php to make it so that it is then changed into a number higher that what is possible.  that is the only thought I really have on this. 

Link to comment
Share on other sites

You can make custom ORDER BY terms using any mysql statement that produces a value. The following should work (you might need to change the DESC to ASC) -

ORDER BY 0 IN (exhib_date_year,exhib_date_month,exhib_date_day) DESC, exhib_date_year DESC, exhib_date_month DESC, exhib_date_day DESC

 

Link to comment
Share on other sites

@litebearer

i designed the database using separate columns because i thought the data would be easier to fetch, in case i wanted to select these date columns'data. as to the invalid date columns, sometimes i insert a row with data regarding an event, and not always do i know when the event is going to exactly happen so i need to let the zeros be accepted.

 

im not sure how you are formatting your dates. in the USA you place the month in a different location than in other countries, so.

anyway, im not sure, but according to my current query, rows are being selected be year first, then month, then day.

 

@PFM

thanks, i will have to look into that and see if that works.thanks

Link to comment
Share on other sites

PFM's is most likely way better than mine (no sarcasm - he really is way better than I) but concatenating year.month.day then sorting that value should get you very close (0 years first, years with 0 month next, years and months with days, then normal dates

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.