Say I have these two tables:
the_schedule_table:
+-----+---------------------+---------------------+-----------+------------+
| id | start_date | end_date | latitude | longitude |
+-----+---------------------+---------------------+------------------------+
| 1 | 2017-07-17 12:00:00 | 2017-07-17 14:00:00 | 42.330078 | -83.045074 |
| 2 | 2017-07-17 8:00:00 | 2017-07-17 11:00:00 | 42.330380 | -83.037918 |
| 3 | 2017-07-17 13:00:00 | 2017-07-17 15:00:00 | 42.351314 | -83.067020 |
| 4 | 2017-07-17 11:00:00 | 2017-07-17 13:00:00 | 42.474879 | -83.241544 |
| 5 | 2017-07-17 15:00:00 | 2017-07-17 17:00:00 | 42.271986 | -83.742137 |
+-----+---------------------+---------------------+-----------+------------+
teams:
+-----+--------+------+
| id | member | mate |
+-----+--------+------+
| 1 | 1 | 2 |
| 2 | 1 | 3 |
| 3 | 1 | 4 |
| 4 | 1 | 5 |
| 5 | 2 | 1 |
+-----+--------+------+
And then, say Id = 1
calls the API with a specific start_date|end_date
and latitude|longitude
, call them target, and wants to know, who are the mates that are within X distance from his data, and their start_date->end_date
DOES NOT overlap with his.
For the overlap I can do this: (StartA > EndB) and (EndA < StartB)
And for the distance I need to combine this: SQL query of Haversine formula
How do I write this sql statement?
EDIT: I have the overlapping part and I tested it and it gave correct results:
// query overlapped activites - based on data time
const myQuery =
"SELECT * FROM schedule " +
"WHERE start_date > '" + toMySqlFormat(target_end_date) +
"' AND '" + toMySqlFormat(target_start_time) + "' > end_date";
Now I get lat,lng and ID, do I check distances then? and finally who are the mates? or do I start by finding mates, then overlaps, then distance?.. etc. I think I am starting right, but I want to optimize it so it is fastest possible. basically 3 conditions:
they are mates (different table)
their time does not overlap (same table)
they are within x distance (same table)
The struggle is real.