如何为多人的日程安排制作mysql视图?

时间:2010-11-08 12:35:20

标签: mysql view

我有一张数据表来描述一个人的预定时间。看起来像这样

Name | day | start_hour | end_hour
-
Tom | Monday   | 8  | 12
Tom | Weekend  | 16 | 20
Dick| Weekdays | 9  | 15
Sue | Sunday   | 7  | 23

如何制作看起来像这样的视图?

Hr | M         | Tu   | W    | Th  | F    | Sa | Su
...
8  | Tom       |      |      |     |      |    | 
9  | Tom, Dick | Dick | Dick | Dick| Dick |    | 
...

每天每小时的视图以及在此期间安排的人员。

3 个答案:

答案 0 :(得分:1)

我会尝试这样的查询。 它会告诉你有多少人可以在start_hour开始工作

start_hour,星期一,星期二 8 2 3

SELECT start_hour, SUM(CASE DAY='Moday' THEN 1 END),
        SUM(CASE DAY='Tuesday' THEN 1 END),
        SUM(CASE DAY='Wednesday' THEN 1 END),
 FROM schedule,
 (
 SELECT 'Monday' dayname FROM dual
 UNION
 SELECT 'Tuesday' dayname FROM dual
 UNION
 SELECT 'Wednesday' dayname FROM dual) days,
 WHERE  days.dayname=schedule.dayname
 GROUP BY start_hour

答案 1 :(得分:1)

SELECT start_hour, 
        GROUP_CONCAT( (CASE DAY='Moday' THEN null ELSE Name  END),
        GROUP_CONCAT( (CASE DAY='Tuesday' THEN null ELSE Name  END),
        GROUP_CONCAT( (CASE DAY='Wednesday' THEN null ELSE Name  END),
 FROM schedule,
 (
 SELECT 'Monday' dayname FROM dual
 UNION
 SELECT 'Tuesday' dayname FROM dual
 UNION
 SELECT 'Wednesday' dayname FROM dual) days,
 WHERE  days.dayname=schedule.dayname
 GROUP BY start_hour

答案 2 :(得分:1)

如果您有hours表(1个整数列hour,值0-23):

SELECT
   hour,
   GROUP_CONCAT(DISTINCT IF(day='Monday'    OR day='Weekdays',name,null) SEPARATOR ','),
   GROUP_CONCAT(DISTINCT IF(day='Tuesday'   OR day='Weekdays',name,null) SEPARATOR ','),
   GROUP_CONCAT(DISTINCT IF(day='Wednesday' OR day='Weekdays',name,null) SEPARATOR ','),
   GROUP_CONCAT(DISTINCT IF(day='Thursday'  OR day='Weekdays',name,null) SEPARATOR ','),
   GROUP_CONCAT(DISTINCT IF(day='Friday'    OR day='Weekdays',name,null) SEPARATOR ','),
   GROUP_CONCAT(DISTINCT IF(day='Saturday'  OR day='Weekend', name,null) SEPARATOR ','),
   GROUP_CONCAT(DISTINCT IF(day='Sunday'    OR day='Weekend', name,null) SEPARATOR ',')
FROM hours 
LEFT JOIN schedule
ON start_hour <= hour AND end_hour > hour OR end_hour = 0
GROUP BY hour;