I’m working on an internal signup form for classes. I am working on a simple function that will check if the user has selected classes with conflicting schedules ( E.g. A class on from 1-2 p.m. as well as a class from 1-3 p.m. on the same day.)
Assuming that I’m using mysqli and fetching the classes the user has selected from a MySQL table with start and end datetime fields, what is the most efficient and/or effective way to compare the multiple classes in order to see if they conflict?
EDIT: Here’s a sample table:
====================================================
| id | start | end |
====================================================
| 1 | 2012-10-01 08:00:00 | 2012-10-01 08:00:00 |
====================================================
Obviously, there will be other data in the table (Like title, description, etc.), but I think I probably only need the data above to do comparisons. I will have an array with the id of every classes the user has signed up for.
Sorry if this is a duplicate, I did search and did not see this asked before.
I’m not sure what your primary key is for this table. I’m fairly confident that mine is wrong for you, but it shouldn’t matter for the purpose of your question.
The first student has an overlap between classes 2 and 3.
Second student, no overlaps.
Third student has an overlap between classes 1 and 2.
If your platform conformed to SQL-92, you could use the OVERLAPS operator. (If any dbms supported assertions, you could declare the tables in such a way that it wouldn’t be possible to insert overlapping classes.)
MySQL doesn’t seem to support that, so you might use the equivalent definition from the SQL standards committee.
I’d expect the join conditions to give you good performance, as long as your tables are indexed carefully. In your case, you’d be selecting rows for a single student, which should be pretty selective. (I didn’t do that in my example code.)