需要来自 MySql 数据库的日期块数组

发布于 2024-12-15 11:11:02 字数 381 浏览 0 评论 0原文

好的,我有一个包含 StartDate 和 EndDate 的行的数据库表。我需要做的是从中返回消耗的时间块。

因此,举例来说,如果我有 3 行,如下所示:

RowID  StartDate   EndDate
1      2011-01-01  2011-02-01
2      2011-01-30  2011-02-20
3      2011-03-01  2011-04-01

那么所用时间块将如下所示:

2011-01-01至2011-02-20和2011-03-01至2011-04-01

有没有一种简单的方法从 MySql 数据库中提取它?欢迎任何建议!

Ok, I have a database table of rows with a StartDate and an EndDate. What I need to do is return blocks of consumed time from that.

So, for example, if I have 3 rows as follows:

RowID  StartDate   EndDate
1      2011-01-01  2011-02-01
2      2011-01-30  2011-02-20
3      2011-03-01  2011-04-01

then the blocks of used time would be as follows:

2011-01-01 to 2011-02-20 and 2011-03-01 to 2011-04-01

Is there an easy method of extracting that from a MySql database? Any suggestions welcome!

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

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

发布评论

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

评论(2

独守阴晴ぅ圆缺 2024-12-22 11:11:02

请看下图,它表示一些重叠的时间段。

X----|              |--------|         |------X
    |-------X      X------------X
                         |----|     X----|

X 标记的任何连续时间段的开始或结束不属于任何其他时间段。如果我们确定这些时间,我们就能取得一些进展。

该查询标识边界。

SELECT boundary FROM
(

-- find all the lower bounds
    SELECT d1.StartDate AS boundary, 'lower' as type
    FROM dates d1
    LEFT JOIN dates d2 ON ( 
        d1.StartDate > d2.StartDate
        AND 
        d1.StartDate < d2.EndDate
    )
    WHERE d2.RowId IS NULL
    GROUP BY d1.StartDate

UNION

-- find all the upper bounds
    SELECT d1.EndDate AS boundary, 'upper' as type
    FROM dates d1
    LEFT JOIN dates d2 ON ( 
        d1.EndDate > d2.StartDate
        AND 
        d1.EndDate < d2.EndDate 
    )
    WHERE d2.RowId IS NULL
    GROUP BY d1.StartDate
) as boundaries

ORDER BY boundary ASC

对您的数据进行此查询的结果是

boundry    | type
------------------
2011-01-01 | lower
2011-02-20 | upper
2011-03-01 | lower
2011-04-01 | upper

您所查询的日期范围位于上面显示的连续下限和上限之间。通过一些后期处理,可以很容易地找到这些。

Look at the diagram below which represents some overlapping time periods

X----|              |--------|         |------X
    |-------X      X------------X
                         |----|     X----|

The beginning or end of any contiguous time period, marked with an X doesn't fall inside any other time period. If we identify these times we can make some progress.

This query identifies the boundaries.

SELECT boundary FROM
(

-- find all the lower bounds
    SELECT d1.StartDate AS boundary, 'lower' as type
    FROM dates d1
    LEFT JOIN dates d2 ON ( 
        d1.StartDate > d2.StartDate
        AND 
        d1.StartDate < d2.EndDate
    )
    WHERE d2.RowId IS NULL
    GROUP BY d1.StartDate

UNION

-- find all the upper bounds
    SELECT d1.EndDate AS boundary, 'upper' as type
    FROM dates d1
    LEFT JOIN dates d2 ON ( 
        d1.EndDate > d2.StartDate
        AND 
        d1.EndDate < d2.EndDate 
    )
    WHERE d2.RowId IS NULL
    GROUP BY d1.StartDate
) as boundaries

ORDER BY boundary ASC

The result of this query on your data is

boundry    | type
------------------
2011-01-01 | lower
2011-02-20 | upper
2011-03-01 | lower
2011-04-01 | upper

The date ranges you are after are between consecutive lower and upper bounds shown abouve. With a bit of post processing, these can easily be found.

抹茶夏天i‖ 2024-12-22 11:11:02

您是否尝试过 mysql group concat

http:// /dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

它将返回一个逗号分隔的字符串,但您仍然需要将其初始化为数组你的 应用。

Have you tried mysql group concat

http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

It would return a comma separated string, but you would still have to intialize that as an array in your application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文