0

I am having a serious issue I can not figure out. I am trying to return rows from MySQL database that have a Start date between two given dates and an End date as well. Here is my php code to query the database:

$getRooms = mysql_query("
    SELECT * 
    FROM Tarifas 
    WHERE Start BETWEEN '2013-01-10' AND '2013-01-13' 
    AND   End   BETWEEN '2013-01-10' AND '2013-01-13'
");

Start and End are set up as DATE fields

my database is set up as follows:

ID | RoomId | Start      | End
--------------------------------------
4  | 34562  | 2013-01-09 | 2013-10-23

If anyone can help me figure out why this is not working, it would be greatly appreciated!!

7
  • 2
    Do you get a PHP or a MySQL error? Commented Sep 9, 2012 at 15:51
  • 1
    Unless the data you have provided is incorrect, you have an end date in the table as 2013-10-23 but you are looking for end dates BETWEEN '2013-01-10' AND '2013-01-13' -- that data doesn't match so you will get no results. Commented Sep 9, 2012 at 15:52
  • Do you have records that meet this criteria? Commented Sep 9, 2012 at 15:52
  • What is the type of the Start and End columns? (Can you give the statement that created the Tarifas table?) Commented Sep 9, 2012 at 15:54
  • Are you checking for date conflicts (e.g. when the whole date range or part of it lies inside a date range stored in database)? Commented Sep 9, 2012 at 15:54

1 Answer 1

3

Looking at your query and sample data, perhaps you are looking for a SQL Query to Find Overlapping or Conflicting Date Ranges. The query would be:

SELECT *
FROM Tarifas 
WHERE '2013-01-13' >= `Start` AND `End` >= '2013-01-10'

2013-01-13 is greater than 2013-01-09 -- and -- 2013-10-23 is greater than 2013-01-10 so Tarifas #4 will be returned since it conflicts/overlaps with the specified dates.

Sign up to request clarification or add additional context in comments.

3 Comments

He only stated that " I am trying to return rows from MySql database the have a Start date between two given dates and an End date as well."
I just inferred this from the sample data and query (and the little knowledge that I have about booking "rooms" and rateplans). The input range is 2013-01-13 thru 2013-01-13 and he PROBABLY wants row id #4 to be returned.
This worked well, I just had to modify it a bit to WHERE start_date<= 'mydate' AND end_date >= 'mydate' Thank you very much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.