Jump to content

order by field()


grantp22

Recommended Posts

Can anybody help out with the following mysql query, I want to order a field by month, but the field values in the database contains the year as well!

Like this eg:

 

+--------------------+--------+---------+

|    strMonth      | event |  place  |

+--------------------+--------+---------+

|February 2011  | ......    | .......    |

|August 2010      | ......    | .......    |

|September 2010| ......    | .......    |

|January 2011    | ......    | .......    |

|October 2010    | ......    | .......    |

|November 2010 | ......    | .......    |

|December 2010 | ......    | .......    |

 

 

This is what I would like to achieve:

 

+--------------------+--------+---------+

|    strMonth      | event |  place  |

+--------------------+--------+---------+

|August 2010      | ......    | .......    |

|September 2010| ......    | .......    |

|October 2010    | ......    | .......    |

|November 2010 | ......    | .......    |

|December 2010 | ......    | .......    |

|January 2011    | ......    | .......    |

|February 2011  | ......    | .......    |

 

 

I could do it like this below if it was just the month, but it will include the year and the year portion can range from 2008 to 2015:

 

SELECT * from myTable order by field(strMonth, 'August', 'September', 'October') asc, strMonth

 

LEFT(strMonth, LENGTH(strMonth)-5) //which should give you eg: August

 

So i thought I could do this:

 

order by field(LEFT(strMonth, LENGTH(strMonth)-5), 'August', 'September', 'October') asc, strMonth

 

But I am sure this will give me:

 

+--------------------+--------+---------+

|    strMonth      | event |  place  |

+--------------------+--------+---------+

|January 2011    | ......    | .......    |

|February 2011  | ......    | .......    |

|August 2010      | ......    | .......    |

|September 2010| ......    | .......    |

|October 2010    | ......    | .......    |

|November 2010 | ......    | .......    |

|December 2010 | ......    | .......    |

 

I just cant figure out how the get them ordered by month and then order by year

 

Can any body help me out with this problem

 

Thanks

Grant

Link to comment
Share on other sites

Hi fenway I figured this was going to be problematic, and my sql is not that hot! The original designer of the website I am upgrading is using the same database for other functionallity, I have been asked to make additions to the website ie: more pages connecting to his db. He no longer works for the company and I have been asked to expand the website.

 

And now I am forced to use these string based fields because the old pages rely on these fields being strings, and I need them to be date based for my additonal pages and now I have run into this problem where I can't change the old page script to date based values because there are so many changes I would need to make.

 

So now I am stuck trying to adapt my own source to accept his old string based db values. I have been trying to figure this out for hours now and even visiting the mysql forums to try and put together a solution. So any ideas you may have will be greatly appreciated at this point no matter how good or bad they are, I getting desperate!

 

Thanks

Grant

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.