Question

I got a table called calendar, in that table I have this rows: -day -month -year

why? Because I need it hehe.

So, the problem is when I want search a date in diferent year ( in the same year it's ok), for example:

Days between: 31-12-2013 and 1-1-2014, so I have a query:

SELECT * FROM calendar WHERE concat(year,'-',month,'-',day) BETWEEN '2013-1-30' AND '2013-1-31';

But, this query search in the same year and show the another months (I have in my db the 730 days) and not the 2014.

So, I'm confused because, how can I concat the rows and search with between? Hope you can help me to understand, and sorry for my english.

Thanks for all.!!

Was it helpful?

Solution

Use str_to_date:

SELECT * FROM calendar 
WHERE str_to_date(concat(year,'-',month,'-',day),'%Y-%m-%d') 
          BETWEEN '2013-1-30' AND '2013-1-31';

sqlfiddle demo

OTHER TIPS

Try

SELECT * FROM calendar WHERE DATE(CONCAT(year,'-',month,'-',day)) BETWEEN '2013-1-30' AND '2013-1-31';

I'm also curious as to why you 'need' to store year, month and day as three seperate fields, and not in a single date field?

I think year, month and day may also be reserved keywords in mysql, so you may have to do

SELECT * FROM `calendar` WHERE DATE(CONCAT(`year`,'-',`month`,'-',`day`)) BETWEEN '2013-1-30' AND '2013-1-31';
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top