高级 MySQL 查询,用于获取同一表中特定日期的记录

发布于 2024-09-12 03:42:27 字数 1552 浏览 6 评论 0原文

我需要找到一种更好、更快速的方法来查询 MySQL 表,以获取给定日期范围内每一天的特定数据,其中表有两个指定开始日期和结束日期的日期列。

表示例:促销 列:

ID   startDate     EndDate     Discount     CategoryID  
=============================================================
1    2010/08/01    2010/08/10     10.00        A
2    2010/08/01    2010/08/10     15.00        B
3    2010/08/11    2010/08/15     05.00        A
4    2010/08/11    2010/08/15     10.00        B

我知道当给出两个日期范围时,我可以从以下查询中获取匹配促销

SELECT * FROM Promotions WHERE (startDate <= "2010/08/12" AND endDate >= "2010/08/15") AND CategoryID = A

上述查询的结果将获取 ID 为 3 的行

但是,如果上述查询中的两个日期范围更改为如下所示

SELECT * FROM Promotions WHERE (startDate <= "2010/08/12" AND endDate >= "2010/08/18") AND CategoryID = A

我没有得到任何匹配的结果,因为我可以理解逻辑与表中存储的任何数据都不匹配。

这就是为什么,我需要一个更好的解决方案,即使结束日期超过促销的结束日期,我也可以获得匹配的结果。然而,改变逻辑不会给我最好的答案,假设如果我使用以下查询我可以获得结果,但是如果我需要找到日期范围内每天的有效折扣,这也不能解决我的整个问题。

SELECT * FROM Promotions WHERE (startDate <= "2010/08/12" AND CategoryID = A

我需要的真正结果是一种将该查询转换为循环的方法,或者使用内存中的临时 MySQL 表来获取每天的结果,如下所示。

我需要找到日期范围 2010/08/12 到 2010/08/18 的结果

===================================================================
Date           Discount    
=========================
2010/08/12      05.00
2010/08/13      05.00
2010/08/14      05.00
2010/08/15      05.00
2010/08/16      null
2010/08/18      null

我希望我的疑问和问题得到很好的解释,如果您需要有关此问题的任何进一步说明,请告诉我。

希望看到一个很好的答案。

I need to findout a better and quick way to query MySQL table to get specific data for each day in a given date range, where the table has two date columns specifying start and end date.

Table example: Promotions
columns:

ID   startDate     EndDate     Discount     CategoryID  
=============================================================
1    2010/08/01    2010/08/10     10.00        A
2    2010/08/01    2010/08/10     15.00        B
3    2010/08/11    2010/08/15     05.00        A
4    2010/08/11    2010/08/15     10.00        B

I know I can grab the match promotion from the following query when two date ranges are given

SELECT * FROM Promotions WHERE (startDate <= "2010/08/12" AND endDate >= "2010/08/15") AND CategoryID = A

The result for the above query will fetch the row with ID 3

However if the two date ranges in the above query was changed to something like the following

SELECT * FROM Promotions WHERE (startDate <= "2010/08/12" AND endDate >= "2010/08/18") AND CategoryID = A

I am not getting any matched result, as I can understand the logic do not match any of the stored data in the table.

That is why, I need a better solution where I can get the result matched even if the end date exceeds more than the end date of a promotion. However changing the logic will not give me the best answer, suppose I can get the result if I use the following query but this also doesn't solve me whole problem if I need to find discounts valid of each and every day in the date range.

SELECT * FROM Promotions WHERE (startDate <= "2010/08/12" AND CategoryID = A

The true result I need is a way to convert this query into a loop or using a temporary MySQL table in the memmory to fetch results for each day as given below.

Result I need to find for the Date Range 2010/08/12 to 2010/08/18

===================================================================
Date           Discount    
=========================
2010/08/12      05.00
2010/08/13      05.00
2010/08/14      05.00
2010/08/15      05.00
2010/08/16      null
2010/08/18      null

I hope my question and problem is well explained and if you need any further clarifications regarding this problem, please let me know.

Hope to see a great answer for this.

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

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

发布评论

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

评论(2

玩套路吗 2024-09-19 03:42:27

您需要一个日期日历表,如下所示:

Create Table Calendar ( [Date] DateTime Not Null Primary Key )

该表是静态的,只需填充一次。它将包含每天一行,涵盖您想要查询的任何日期范围。这样,您的查询就更简单了:

Select C.Date, P.Discount
From Calendar As C
    Left Join Promotions As P
        On C.Date Between P.StartDate And P.EndDate
            And P.Category = 'A'

You need a calendar table of dates that looks something like:

Create Table Calendar ( [Date] DateTime Not Null Primary Key )

This table would be static and would need only be populated once. It would contain one row for each day covering any date range on which you would want to query. With that, your query is simpler:

Select C.Date, P.Discount
From Calendar As C
    Left Join Promotions As P
        On C.Date Between P.StartDate And P.EndDate
            And P.Category = 'A'
邮友 2024-09-19 03:42:27

要查询日期范围内的项目,请使用:

SELECT * 
FROM Promotions 
WHERE (startDate >= "2010/08/12" AND endDate <= "2010/08/15") 
AND CategoryID = A

您希望开始日期大于或等于,结束日期小于或等于。

To query for items that are IN the date range, use:

SELECT * 
FROM Promotions 
WHERE (startDate >= "2010/08/12" AND endDate <= "2010/08/15") 
AND CategoryID = A

you want the start date the be greater than or equal, and the end date to be less than or equal.

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