Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to return the closest time slots grouped by an entity, but just for one closest available day, not every available day?
    primarykey
    data
    text
    <p>I have a table that stores available appointments for a teacher, hourly based, with total freedom to add unlimited slots per day per teacher (as long as slots don't overlap). Example simplified structure:</p> <pre><code>CREATE TABLE time_slots ( id int(10) unsigned NOT NULL AUTO_INCREMENT, teacher_id mediumint(8) unsigned NOT NULL, slot bigint(20) unsigned NOT NULL DEFAULT '0', ); </code></pre> <p>The <strong>slot</strong> column stores the timestamp of the slot.</p> <p>How can I show all available slots for the first closest available day for every teacher? Notice: show all time slots of a given day and can't show more than one day for the same teacher.</p> <h2>Example data:</h2> <p>PS: using datetime just to make things more readable.</p> <pre><code>+----+------------+------------------+ | id | teacher_id | slot | +----+------------+------------------+ | 1 | 1 | 2013-04-10 08:00 | | 2 | 1 | 2013-04-10 09:00 | | 3 | 1 | 2013-04-10 09:30 | | 4 | 1 | 2013-04-11 08:00 | | 5 | 1 | 2013-04-11 09:00 | | 6 | 1 | 2013-04-11 10:30 | | 7 | 2 | 2013-04-12 07:00 | | 8 | 2 | 2013-04-12 09:00 | | 9 | 2 | 2013-04-14 08:00 | +----+------------+------------------+ </code></pre> <h2>Expected result:</h2> <p>Assuming a search is made on: <strong>2013-04-10 08:30</strong>, the returned results have to be:</p> <pre><code>+----+------------+------------------+ | id | teacher_id | slot | +----+------------+------------------+ | 2 | 1 | 2013-04-10 09:00 | | 3 | 1 | 2013-04-10 09:30 | | 7 | 2 | 2013-04-12 07:00 | | 8 | 2 | 2013-04-12 09:00 | +----+------------+------------------+ </code></pre> <ol> <li>Don't show id 1 because it is already past 08:00 from day 10/Apr.</li> <li>Don't show id 4, 5, 6 because we found the closest available slots for teacher = 1 to be id's 2 and 3.</li> <li>Don't show id 9 because for teacher = 2 the closest available slots are the 7 and 8, so don't return another day.</li> </ol> <h2>What I tried</h2> <p>I'm failing hard to come with a query, I just came with this basic query, but it makes no assumptions on getting just the 1st available day, and of course it doesn't return all available slots in a given day. It just returns 1 slot per teacher:</p> <pre><code>SELECT id, teacher_id, FROM_UNIXTIME(slot) FROM time_slots WHERE slot &gt;= [Actual timestamp] GROUP BY DATE(FROM_UNIXTIME(slot)) ORDER BY slot, teacher_id ASC </code></pre> <p>Note: I'm using FROM_UNIXTIME here just for purposes of debugging, of course I'll optimise indexes, etc later.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload