gpt4 book ai didi

mysql - 如何从日历数据库中获取可用时间段列表

转载 作者:行者123 更新时间:2023-11-29 03:34:05 24 4
gpt4 key购买 nike

我有一个事件/日历 MySQL 表,其中每个用户全天都有多个约会/事件。如果一个用户不能进行那个约会/事件“因为他/她在其他约会上落后了”,我需要能够将这个约会重新分配给另一个可用的用户。因此,我需要显示在预定时间范围内可用且可以接受此约会的前 5 名用户的建议,经理将能够将此约会重新分配给建议的用户之一。

我的事件表看起来像这样

CREATE TABLE `calendar_events` (
`event_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`start_on` datetime NOT NULL,
`end_on` datetime NOT NULL,
`subject` varchar(255) NOT NULL,
`event_type` enum('Phone Call','Meeting','Event','Appointment','Other') CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL DEFAULT 'Phone Call',
`all_day_event` tinyint(1) DEFAULT '0' COMMENT '1 = all day event, 0 = no',
`phone_call_id` int(11) unsigned DEFAULT NULL,
`account_id` int(11) unsigned DEFAULT NULL,
`client_id` int(11) unsigned DEFAULT NULL,
`owner_id` int(11) unsigned NOT NULL,
`created_by` int(11) unsigned NOT NULL,
`created_on` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`modified_by` int(11) unsigned DEFAULT NULL,
`modified_on` datetime DEFAULT NULL,
`event_location` varchar(255) DEFAULT NULL,
`event_notes` varchar(10000) DEFAULT NULL,
`status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '0 = purged, 1 = active, 2=pass, 3 = cancled, 5 = waiting for auditor to be enabled',
PRIMARY KEY (`event_id`),
UNIQUE KEY `phone_call_id` (`phone_call_id`,`account_id`,`client_id`),
KEY `client_id` (`client_id`),
KEY `account_id` (`account_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

所以让 event_id = 100 分配给 user_id = 2 并安排到 start_on = '2014-09-21 10:00:00' 和 end_on '2014-09-21 10:00:00'

and user_id = 5 有约会 start_on '2014-09-21 11:45:00' 和 end_on '2014-09-21 12:30:00'

并且 user_id = 2 无法完成他安排在“2014-09-21 10:00:00”的约会,因此他们的系统将建议 user_id = 5,因为他将在接下来的 105 分钟内。

最终的数据集需要是

event_id org_owner  suggested_owner   available_for
100 2 5 105

如果用户安排了一个事件(一个用户可以有多个记录),下面的查询将从 users 表中为我提供所有可用用户的列表以及 start_on end_on 值。此查询中的 start_on 为 null,这意味着此用户没有任何事件,否则它将返回每个事件的开始。

所以如果用户 ID 出现在上面的查询中并且在 start_on 列中有一个 NULL 值,这意味着这个用户全天都有空所以这个用户应该是推荐的 5 个用户中的一个,因为它是最高的可用性。但是如果一个用户在数据集中有一行/多行在start on then中有一个非空值,我们需要查看距离事件最近的start_on,然后推荐具有最大可用性的前5个值(value)。

SELECT user_id, start_on, end_on, subject
FROM view_users AS su
LEFT JOIN calendar_events AS c ON c.owner_id = su.user_id AND c.start_on NOT BETWEEN '2014-09-30 00:00:00' AND '2014-09-30 23:59:59' AND c.status = 1
WHERE su.is_available_today = 1

我怎样才能提取这个数据集?

最佳答案

感谢您的帮助编辑了第一个提案,只需要照顾没有任何事件的用户(可以通过在“t”子查询中进行左连接来实现)。这可以改进很多,但现在我有点累了:)

SELECT
c.event_id, -- Event id
c.owner_id AS org_owner, -- Original owner of event
t.owner_id AS suggested_owner, -- Suggested new user
c.start_on, -- Event start
t.free_from, -- Owner free slot start
t.free_to, -- Owner free slot end
TIME_TO_SEC( TIMEDIFF( t.free_to, c.start_on ) ) /60 AS available_for -- Availibility of minutes (diff between event start and free slot end)

FROM calendar_events AS c

-- Join with free slots
LEFT JOIN (
-- Add a slot for beginning, 1999-01-01 to first event start
SELECT * FROM (
SELECT owner_id, '1900-01-01' AS free_from, MIN( start_on ) AS free_to
FROM calendar_events c3
GROUP BY owner_id
) AS deb

UNION

-- select free slots by taking the event end and the following event start
SELECT owner_id, `end_on` AS free_from, (
SELECT start_on
FROM calendar_events c2
WHERE c2.owner_id = c1.owner_id
AND c2.start_on > c1.end_on
ORDER BY c2.start_on
LIMIT 0 , 1
) AS free_to

FROM calendar_events c1

UNION

-- Add a slot for end, last event end to 2100-01-01
SELECT * FROM (
SELECT owner_id, MAX( end_on ) AS free_from, '2100-01-01' AS free_to
FROM calendar_events c3
GROUP BY owner_id
) AS end
) AS t ON t.owner_id <> c.owner_id
-- Join avoid using same user and ensure free slot matches event dates
AND t.free_from <= c.start_on AND t.free_to >= c.end_on
WHERE c.status = 1
AND c.event_id =52
GROUP BY t.owner_id -- To avoid multiple free slots by user
ORDER BY available_for DESC -- Sort to list biggest slots first
LIMIT 0, 5 -- Only five first matching users

祝你好运:)

关于mysql - 如何从日历数据库中获取可用时间段列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25962807/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com