Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 965949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:04:55+00:00 2026-05-16T02:04:55+00:00

I need to findout a better and quick way to query MySQL table to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T02:04:55+00:00Added an answer on May 16, 2026 at 2:04 am

    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'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a quick way to find out if a given port is open
I have a table with 10 columns that need to be searchable (the table
I need to get the content of folder deleted from our repository long time
Sometimes I need to copy MySQL database (db1) to another database (db2). I found
I'm storing results from a quiz in a table, and I need to find
I need a way for my java layer to inspect what constraints exist for
I need to read several barcode scanners and bind the read data according to
I need some way to find out when the user released the mouse button
I need to find out how to format numbers as strings. My code is
I need to find out the time a function takes for computing the performance

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.