Guest User

Untitled

a guest
Feb 21st, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 1.59 KB | None | 0 0
  1. SET @time = '2011-27-10 14:00:00';
  2. SET @stud = 4;
  3.  
  4. SELECT id_teacher
  5. FROM bb_appointment root
  6. WHERE id_student = @stud
  7. AND id_teacher NOT IN (
  8.     SELECT id_teacher
  9.     FROM bb_appointment
  10.     WHERE time = @time
  11. --) AND 3 > (
  12. --    SELECT count(*) AS hours
  13. --    FROM (
  14. --        SELECT id_teacher
  15. --        FROM bb_appointment
  16. --        WHERE id_teacher = root.id_teacher
  17. --    ) as lambda
  18. ) AND id_teacher NOT IN (       -- this is definitely the last resort.
  19.     SELECT id_teacher           -- TODO: make a better solution faggot
  20.     FROM (
  21.         SELECT id_teacher, count(*) AS hours
  22.         FROM (
  23.             SELECT id_teacher   -- select all teachers work events of today
  24.             FROM bb_appointment
  25.         ) as lambda
  26.         GROUP BY id_teacher     -- calculate total hours for each teacher
  27.         ORDER BY hours ASC
  28.     ) as lambda
  29.     WHERE hours > 2             -- and reject any results with more than two hours
  30. );
  31.  
  32. -- TODO: Merge these somehow
  33.  
  34. SELECT id                       -- START SELECTING LEAST ACTIVE TEACHER TODAY
  35. FROM (
  36.     SELECT id, count(*) AS cnt
  37.     FROM (
  38.         SELECT id               -- grab ID of all teachers in database
  39.         FROM bb_teacher
  40.         UNION ALL --=--         -- lazily concatenate with
  41.         SELECT id_teacher
  42.         FROM bb_appointment     -- ID of teachers printed once for each hour of activity
  43.     ) AS lambda
  44.     GROUP BY id                 -- sort by ID and count occurence of each ID, giving grand total +1
  45.     ORDER BY cnt ASC
  46. ) AS lambda
  47. LIMIT 1                         -- END SELECTING LEAST ACTIVE TEACHER TODAY
Add Comment
Please, Sign In to add comment