tags:

views:

56

answers:

2

im trying to show items booked between two separate date columns (start and end dates). i want to search between the dates but the query seems to ignore any dates that land between the selected dates.

SELECT
bookings.booking_id,
bookings.booking_id,
bookings.booked_from,
bookings.booked_from,
bookings.booked_till,
item_set_computer.item_id,
item_set_computer.name,
item_set_computer.booking_groups_id
FROM
bookings,
item_set_computer
WHERE
item_set_computer.item_id = bookings.item_id
AND
item_set_computer.booking_groups_id = 3
AND
booked_from
BETWEEN "2010-04-13" AND "2010-04-20"
AND
booked_till
BETWEEN "2010-04-13" AND "2010-04-20"

for instance, I have an item booked from 13th to 15th. date1 is 2010-04-13 and date2 is 2010-04-15. User searches for booked items from 14th to 16th. It returns no results. Why is it ignoring database dates that drop between dates selected by the user? The columns are set as DATE in the database and have been correctly entered.

A: 

are there recordsets with the same id in both table which falls under the clause? also make sure that with WHERE col BETWEEN a AND b, a has to be smaller than b to work.

what gets returned if you comment out the last 6 lines?

knittl
it returns results limited to the booking group and bookings made. Yes there are record sets with the same item_id.
Mark
+3  A: 

You said "User searches for booked items from 14th to 16th."

That means your query will be

AND
booked_from
BETWEEN "2010-04-14" AND "2010-04-16"
AND
booked_till
BETWEEN "2010-04-14" AND "2010-04-16"

The first AND clause will obviously be false (since your 4/13 start date is NOT indeed between 4/14 and 4/16)

The correct logic for you (assuming you want the FULL booking interval) is

AND booked_from >= "2010-04-14" 
AND booked_till <= "2010-04-16" 

Or if you want partial interval

AND booked_from >= "2010-04-16" -- means we booked before the end of interval
AND booked_till <= "2010-04-14" -- means we left after the start 
DVK
+1 Beat me to it!
Matt Ellen
+1. Overlapping booking interval would be `booked_from <= '2010-04-16' AND booked_till >= '2010-04-14'`
Quassnoi
@Quassnoi - lol good point, I was typing it up when you wrote the comment :)
DVK
And of course using `SPATIAL` abilities would greatly improve this query. http://explainextended.com/2009/07/01/overlapping-ranges-mysql/
Quassnoi
ok slightly confused, would like overlapping bookings to be flagged in my query as well as those that fall inside it.
Mark
do i need both AND booked_from >= "2010-04-14" AND booked_till <= "2010-04-16"as well asAND booked_from >= "2010-04-16"AND booked_till <= "2010-04-14"
Mark
This is the php im using, ive entered (this will look like a mess, cant format! -also i know about not using $_POST in sql)$bookedItems = "SELECT bookings.booking_id, bookings.booked_from, bookings.booked_till, ".$itemSetLong.".item_id, ".$itemSetLong.".name, ".$itemSetLong.".booking_groups_id FROM bookings, ".$itemSetLong."WHERE ".$itemSetLong.".item_id = bookings.item_id AND ".$itemSetLong.".booking_groups_id = ".$groupId."AND booked_from >= '".$_POST['startDate']."'AND booked_till <= '".$_POST['endDate']."'
Mark
well spotted, dude
knittl