tags:

views:

141

answers:

3

Hello all,

I currently have 3 tables, which I'm using for people to make reservations for certain pieces of equipment.

Here are my tables:

tblEquipment:
     id        name        description
     1       Camera        Takes pictures
     2       Projector      Projects pictures
     3       Laptop         Portable Computer


tblEvents:
     id        start                 end              first_name         last_name              email
     1     2009-08-10      2009-08-11          John                 Doe                 [email protected]
     2     2009-08-15      2009-08-16          Jane                 Doe                 [email protected]


tblEventData:
     id         eventID             equipmentID
     1              1                         1             
     2              1                         2

Right now, a user will submit a query with their requested times, then they will see all available equipment.

So, using the exampe above, if a user is looking for equipment between 8/10-8/11, he will see that the only equipment that is available is: equipmentID 3 (Laptop).

How can I create my query to return only the available equipment based on the requested times?

This is what I've come up with so far, but can't get it to work:

SELECT tblequipment.id as name, tblEvents.start as start, tblEvents.end as end
FROM tblEquipment
INNER JOIN tblEventData on tblEventData.equipmentID = tblEquipment.id
INNER JOIN tblEvents on tbleventdata.eventID = tblEvents.id
WHERE NOT EXISTS(SELECT * FROM tblEvents WHERE $end >= start AND $start <= end)

Any ideas? Thanks!

+1  A: 

The query you have now has a NOT EXISTS looking only for events that occur between the start and end times given. In other words, "select all equipment as long as there are no events in this timeframe." That's not what you want. You want: "select all equipment as long as there are no events using that equipment in this timeframe."

That translates to something like:

SELECT tblequipment.id as name
FROM tblEquipment
WHERE NOT EXISTS 
  (SELECT * FROM tblEvents 
   INNER JOIN tblEventData ON (tblEvents.id = tblEventData.eventID)
   WHERE $end >= start AND $start <= end
   AND tblEventData.equipmentID = tblEquipment.id)

EDIT: I've also removed the JOINs from the outer query, since they insist that you select only equipment that's reserved at some point, which is not at all relevant to the question you're trying to answer.

You do want to know what equipment is reserved, but inside the NOT EXISTS query, for the purposes of excluding it from your final results.

VoteyDisciple
Thanks for the reply, VoteyDisciple, however, I now get the error: Unknown column 'tblEquipment.equipmentID' in 'where clause' Any idea?
I misread your original schema. There is indeed no column by that name; you've named it tblEquipment.id — I will make the appropriate edit, along with another change.
VoteyDisciple
that last portion should just be tblEventData.equipmentID = tblEquipment.id
localshred
Changed worked perfect, thanks!
+1  A: 

Try putting end in quotes so it is

`end`

I think mysql is interpreting end as a command rather than a field.

James
+1  A: 

"end" is a reserved word in SQL. Try naming tblEvents.end something else.

seventeen
Good point, thx. I went ahead and changed the column names (since I'm early enough into the project) to startTime and endTime.
Dodinas