使用MySQL 5.6查找重叠的日期范围记录?

发布于 2025-01-19 17:41:16 字数 720 浏览 4 评论 0原文

我有一个带有日期范围和场地ID的活动列表。 我需要同时找到不同主动场所的最大计数。

create table if not exists events
(
    eventstart datetime,
    eventend datetime,
    venue int
);

INSERT INTO events VALUES ('2022-04-06 18:00:00', '2022-04-06 19:30:00', 1);
INSERT INTO events VALUES ('2022-04-06 18:15:00', '2022-04-06 21:00:00', 1);
INSERT INTO events VALUES ('2022-04-06 18:45:00', '2022-04-06 20:00:00', 2);
INSERT INTO events VALUES ('2022-04-06 18:30:00', '2022-04-06 19:00:00', 3);

https:///www.db-fiddle.com/ >

在上一个示例中,我期望3个结果,因为从18.45-19.00起3个同时活跃的并发不同的场所。

我找不到写此查询的聪明方法。

I have a list of events with date-range and venue id.
I need to find the max count of distinct active venues at the same time.

create table if not exists events
(
    eventstart datetime,
    eventend datetime,
    venue int
);

INSERT INTO events VALUES ('2022-04-06 18:00:00', '2022-04-06 19:30:00', 1);
INSERT INTO events VALUES ('2022-04-06 18:15:00', '2022-04-06 21:00:00', 1);
INSERT INTO events VALUES ('2022-04-06 18:45:00', '2022-04-06 20:00:00', 2);
INSERT INTO events VALUES ('2022-04-06 18:30:00', '2022-04-06 19:00:00', 3);

https://www.db-fiddle.com/f/a2PRXG2WQ3samrWb66fimv/4

With the previous example, I expect a result of 3 because from 18.45-19.00 there 3 concurrent distinct venues active at the same time.

I cannot find a smart way to write this query.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文