Jump to content

[SOLVED] date range selection issue in mysql (when compared in two columns of the table)


Recommended Posts

Hi all,

 

I am facing an issue with MYSQL query.

 

I am having a table named “report_hours”

 

Id Category_type hours start_date end_date

1 2 2 2009-05-02 2009-05-03

2 4 23 2009-05-02 2009-05-07

3 6 12 2009-04-02 2009-05-07

 

From the above table I am generating a report, where the user will be prompted for “From Date” and “To Date”

 

Now I would like to retrieve the records which are between the dates given by the user

 

I have tried some options with keyword “between clause” in mysql but I didn’t get the exact results, when the user has selected From date as “2009-04-01” and To date as “2009-05-08”

 

I have listed down the query which I have tried below:

 

Query no 1:

SELECT * FROM ` report_hours ` WHERE (`start_date` BETWEEN '2009-04-01' AND '2009-05-08')

OR (`end_date` BETWEEN '2009-04-01' AND '2009-05-08')

 

Query no 2:

SELECT * FROM ` report_hours ` WHERE (DATE('2009-04-01') BETWEEN DATE(start_date) AND DATE(end_date) )OR (DATE('2009-05-08') BETWEEN DATE(start_date) AND DATE(end_date)

))

 

 

I need all the 3 records to be displayed when the above date has been selected. Is there any option available in mysql itself to search for date in a range (between two columns?). Need help on this.

 

Regards

Srinivasan

 

Both of you thanks for the reply.

 

finally, I found the solution

 

SELECT *

FROM `report_hours`

WHERE (

(

start_date >= '2009-04-01' && start_date <= '2009-05-08'

)

OR (

end_date >= '2009-04-01' && end_date <= '2009-05-08'

)

OR (

'2009-04-01' BETWEEN start_date AND end_date

 

OR '2009-05-08' BETWEEN start_date AND end_date

)

)

 

the query which "vintox" sent has not worked for few combinations, but it gave us the idea to find out the solution. Still the testing is going on.

 

Thanks a lot.

 

Regards,

Srinivasan.C

 

 

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.