I’m currently working on an Equipment Reservation System for my school.
Here’s basically what my tables look like:
tblEquipment:
id name description
1 Camera Takes pictures
2 Projector Projects images
3 Stereo Plays music
tblEvents:
id equipmentID start end
1 2,3 1251312300 1251315900 //Should I use comma delimited entries for equipmentID?
2 1 1251312300 1251315900
Regarding my project, I have a couple of questions:
1) If multiple pieces of equipment are being reserved, (which will happen more times than not) should the “equipmentIDs” be comma delimited in the equipmentID field?
2) Currently, when a user makes a reservation, they first select their “requested times”, then are presented with available items at that time. Here’s what I am using for that query:
$start = //user's requested time
$start = //user's requested time
SELECT equipmentID FROM tblEvents
WHERE ($start >= start && $start <= end)
OR ($end >= start && $end <= end)
OR ($start <= start && $end >= end
while($row = mysql_fetch_array($data)) {
echo $row['equipmentID']; //Would echo something like:
echo "<br>"; // 2,3
// 1
}
My question is this:
How can I take the ‘results’ of the above query to then re-query the ‘tblequipment’ table, but exclude the items that were in the ‘results’ above (because they would not be available). Keeping in mind, that my query above may return multiple rows.
Any help on this would be great, thanks!
Regarding #1: No! No, no, no, no, no! If you have multiple pieces of equipment being reserved, then you should have multiple rows in the reservations table (what looks like
tblEventshere). To avoid duplicating the other fields intblEvents, you’d typically create a third table, perhapstblEventEquipmentthat simply lists what equipment belongs with what event.If you need a comma-separated list for the purposes of output (which doesn’t seem likely), you can always generate one with
GROUP_CONCAT(), but inside the table you want one row per reserved piece of equipment. Otherwise, SQL cannot effectively (or efficiently) determine what equipment is reserved at a particular time.Regarding #2, you want a query like:
This selects equipment for which there isn’t a reservation. Note that I simplified your logic for determining whether the equipment is reserved or not by doing just two comparisons.
Finally, an unrelated note: I recommend strongly against storing timestamps as integers in the database table. Use MySQL’s built-in
DATETIMEtype. If you need to convert to/from a timestamp, you can use theUNIX_TIMESTAMP()andFROM_UNIXTIME()functions.