Query to get the next free slot #71
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
We need to get the
next_free_slot
, and this needs to take into account the eps and reservations table.This gives the next free slot from the eps table
SELECT id + 1 FROM eps mo WHERE NOT EXISTS ( SELECT NULL FROM eps mi WHERE mi.id = mo.id + 1 ) ORDER BY id LIMIT 1"
But that also needs to be combined with the reservations table
SELECT id as ep_num FROM eps UNION ALL ( SELECT ep_num FROM reservations ) ORDER BY ep_num DESC
The
hpr.sql
file can be downloaded at https://hackerpublicradio.org/hpr.sqlThe following UNIONS the existing episodes from the two tables from the current date forward. It generates a list of all dates from the current date to the furthest reserved date and then determines the next open slot by taking the closest date with no episode from the LEFT JOIN of list of all future dates to list of reserved dates:
Tested MySQL 8.0
Tested with SQLite 3.37
Wow that's complex
Yes, the difficult part is generating the full range of dates. It could be simplified some by changing how the recursive function limit is generated. Instead of finding the number of days between the reserve_max and reserve_min, we could just say generate 365 days worth of dates from the current date forward. This would remove the reserve_max and reserve_min cte's.
So the WHERE clause for generate_upcoming_dates goes from:
to:
Perhaps we should try approaching it from a different angle.
Ignore the dates and just get a list of numbers from one table, and a list of numbers from another table, merge them, then find the first gap.
It might be simpler to do this in php, Or get the
$first_free_slot
fromeps
then do a loop onreservations
to see if the episode number is been worked on.Yes, doing it from PHP like that is easy enough. The SQL from the first CTE, reserved_dates, gives you the list of dates to have been taken:
I do like DB solutions as I find I end up rewriting front end code more often than DB queries, but HPR code is probably more stable than other projects I have helped--so not as much an issue.
We only need the eps
Can the
SELECT id + 1 FROM eps mo WHERE NOT EXISTS ( SELECT NULL FROM eps mi WHERE mi.id = mo.id + 1 ) ORDER BY id LIMIT 1"
be run over this list