Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SET @time = '2011-27-10 14:00:00';
- SET @stud = 4;
- SELECT id_teacher
- FROM bb_appointment root
- WHERE id_student = @stud
- AND id_teacher NOT IN (
- SELECT id_teacher
- FROM bb_appointment
- WHERE time = @time
- --) AND 3 > (
- -- SELECT count(*) AS hours
- -- FROM (
- -- SELECT id_teacher
- -- FROM bb_appointment
- -- WHERE id_teacher = root.id_teacher
- -- ) as lambda
- ) AND id_teacher NOT IN ( -- this is definitely the last resort.
- SELECT id_teacher -- TODO: make a better solution faggot
- FROM (
- SELECT id_teacher, count(*) AS hours
- FROM (
- SELECT id_teacher -- select all teachers work events of today
- FROM bb_appointment
- ) as lambda
- GROUP BY id_teacher -- calculate total hours for each teacher
- ORDER BY hours ASC
- ) as lambda
- WHERE hours > 2 -- and reject any results with more than two hours
- );
- -- TODO: Merge these somehow
- SELECT id -- START SELECTING LEAST ACTIVE TEACHER TODAY
- FROM (
- SELECT id, count(*) AS cnt
- FROM (
- SELECT id -- grab ID of all teachers in database
- FROM bb_teacher
- UNION ALL --=-- -- lazily concatenate with
- SELECT id_teacher
- FROM bb_appointment -- ID of teachers printed once for each hour of activity
- ) AS lambda
- GROUP BY id -- sort by ID and count occurence of each ID, giving grand total +1
- ORDER BY cnt ASC
- ) AS lambda
- LIMIT 1 -- END SELECTING LEAST ACTIVE TEACHER TODAY
Add Comment
Please, Sign In to add comment