如何制作一个mysql视图来查看多人的日程?

发布于 2024-10-01 03:10:32 字数 440 浏览 1 评论 0原文

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

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 |    | 
...

每天每小时的视图以及该时间段内安排的人员。

I have a table of data to describe a person's scheduled time. It looks like this

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

How can I make a view that looks like this?

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

A view of each hour of each day and who is scheduled during that time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

兔小萌 2024-10-08 03:10:32

如果您有 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;

If you have the hours table (1 integer column hour, values 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;
樱娆 2024-10-08 03:10:32

我会尝试类似这样的查询。
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

I would try something like this query.
It would give you how many guys could start to work at start_hour

start_hour, monday, tuesday
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
喜爱纠缠 2024-10-08 03:10:32
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
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文