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 7810075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:38:42+00:00 2026-06-02T03:38:42+00:00

I have a table that tracks emails sent. It is pretty simple. ID |

  • 0

I have a table that tracks emails sent. It is pretty simple.

ID | DATETIME | E-MAIL | SUBJECT | MESSAGE

I have been collecting data for several years. Some days I don’t have any entries in the table.

query1:

SELECT COUNT(ID) FROM emails
WHERE DATE(datetime) >= 'XXXX-XX-XX'
AND DATE(datetime) is <= 'ZZZZ-ZZ-ZZ'
GROUP BY DATE(datetime)

I then use a some php to get one year prior for both XXXX and YYYY and run the second query which is the same as the first…

query2:

SELECT COUNT(ID) from emails
WHERE DATE(datetime) >= 'XXXX-XX-XX'
AND DATE(datetime) is <= 'ZZZZ-ZZ-ZZ'
GROUP BY DATE(datetime)

I am using a charting package to compare how many emails I got for a date range and then I overlay how many emails I got for the same range only one year prior. This is two queries right now and I chart the results.

The issue is where mysql does not have any emails for 2011 for a day in question, but has a few in 2012 for the same day.

Combining the results and graphing them skews the results since I am missing a date and a 0 value for last year for that day, effectively making all my values no longer match up.

2011-03-01 10    2012-03-01 4
2011-03-02 4     2012-03-02 2
2011-03-03 6     2012-03-04 1  <---- see where the two queries
                               end up diverging? (I had nothing
                               logged for 2012-03-03 so naturally
                               it was not in the results.

Is there a way I can get mysql to output the data I need including dates where value appear in one year but not another OR if no values appear in either year (still need date and 0) so my chart works?

I cannot seem to figure out how to do this…

Thanks!

  • 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-06-02T03:38:42+00:00Added an answer on June 2, 2026 at 3:38 am

    There are a few different ways to get the results for a contiguous set of dates. My favourite one is to create the full set that is required using a dummy table or an existing contiguous set of ids from an AI PK. Something like this –

    SELECT '2011-01-01' + INTERVAL (id -1) DAY
    FROM dummy
    WHERE id BETWEEN 1 AND 365
    

    This will return a full set of days for 2011 which can then be LEFT JOINed to your emails table to get the counts –

    SELECT `dates`.`date`, COUNT(emails.id)
    FROM (
        SELECT '2011-01-01' + INTERVAL (id - 1) DAY AS `date`, '2011-01-01 23:59:59' + INTERVAL (id - 1) DAY AS `end_of_day`
        FROM dummy
        WHERE id BETWEEN 1 AND 365
    ) `dates`
    LEFT JOIN emails
        ON `emails`.`datetime` BETWEEN `dates`.`date` AND `dates`.`end_of_day`
    GROUP BY `dates`.`date`
    

    To populate your dummy / seq table you can insert the first ten values manually and then use INSERT … SELECT to add the rest –

    CREATE TABLE dummy (id INTEGER NOT NULL PRIMARY KEY);
    INSERT INTO dummy VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10);
    SET @tmp := (SELECT MAX(id) FROM dummy) + 1;
    INSERT INTO dummy
    SELECT @tmp + id
    FROM dummy;
    

    You need to execute the SET query before each run of the INSERT … SELECT query.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table where I save emails that have been sent. I decided
I have a table that tracks emails sent from applications on my server. I
I have a History Table in SQL Server that basically tracks an item through
I have table that I insert data with following query (from c# code): INSERT
I have a table that has redundant data and I'm trying to identify all
I have a table that tracks attendance in a course. The columns are the
I have a table that tracks serial numbers and the number of licenses associated
So I have a table that tracks the updating of a projects status. Now
I have this big database table that tracks (currently in production), a persons medical
I have a table (tbl_MatterItem) in my DB that tracks the dates that items

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.