Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think there is no 'best' answer to your question. I give you some schema but there could be more answers which would be good as well.</p> <pre><code>shedule_map schedule activity ------------------------------------------------ id id id user_id schedule_id schedule_id default_schedule_id user_id date startOfWeek start_hour end_hour activity_description </code></pre> <p>'Schedule' table every week will gain one entry per user. If user choose default schedule for current week in 'schedule' table schedule_id will be obtained from schedule_map. Elswhere schedule_id = MAX(schedule_id) + 1 and new activities for this new schedule will be created. Lets get today's activities from that schema for user_id = 10:</p> <pre><code>SELECT act.start_hour, act.end_hour, act.activity_descripton FROM activity act INNER JOIN schedule s USING (schedule_id) WHERE s.startOfWeek = ( SELECT MAX(startOfWeek) FROM schedule WHERE startOfWeek &lt; NOW() ) AND s.user_id = 10 ORDER BY act.start_hour </code></pre> <p>I use mostly ORMs and there are problems with subqueries so startOfWeek adequate to current date you can obtain from other query.</p> <p>I don't think one additional entry per user per week can be a problem. But if you think it is then you can add to 'schedule_map' table column 'custom_schedule_id' setting it to NULL if user choose default schedule. But then you would have to determine firstly if user use custom schedule or not or more complicated query will be needed. On example </p> <pre><code>SELECT act.start_hour, act.end_hour, act.activity_descripton FROM activity act INNER JOIN schedule s ON act.schedule_id = s.schedule_id INNER JOIN schedule_map sm ON sm.user_id = s.user_id AND sm.custom_schedule_id = s.schedule_id WHERE s.user_id = 10 ORDER BY act.start_hour UNION ALL SELECT act.start_hour, act.end_hour, act.activity_descripton FROM activity act INNER JOIN schedule s ON act.schedule_id = s.schedule_id INNER JOIN schedule_map sm ON sm.user_id = s.user_id AND sm.default_schedule_id = s.schedule_id AND sm.custom_schedule_id IS NULL WHERE s.user_id = 10 ORDER BY act.start_hour </code></pre>
    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.
    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