MySQL Forums
Forum List  »  Newbie

Re: problem for selecting range of date between 2 dates
Posted by: Rick James
Date: March 22, 2012 07:59PM

CONCAT won't work because you don't have leading zeros on the month:
Last September would come after October: "20119" > "201110"

CONCAT(year, RIGHT(CONCAT('0', month), 2))
is a clumsy workaround for the leading zero issue.

12+year+month world work universally. (Ditto for 100*year+month.)

> ( year >= @start_year and mon >= @start_mon OR year > @start_year )
is incorrect. The AND will happen before the OR, so you need:
( year >= @start_year and ( mon >= @start_mon OR year > @start_year ) )
That is even reasonably efficient if you have
INDEX(year, com)

This should also work:
WHERE (year, month) >= (2011, 10) AND (year, month) <= (2012, 2)

None of the above scales well. (Indexes will not be used, or not used very well.)

Recommend you
1. ALTER TABLE ADD another column to contain the combined year_month.
2. UPDATE to set that new column
3. ALTER TABLE foo DROP COLUMN year, DROP COLUMN month;
The life will be much simpler!

Options: ReplyQuote




Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.