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

  • Home
  • SEARCH
  • 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 1110753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:26:32+00:00 2026-05-17T02:26:32+00:00

I have this query to count views in a table, by date. It obviously

  • 0

I have this query to count “views” in a table, by date. It obviously gives me only the dates where there has been activity, but I wish I could have MySQL return data for a selected period of time. For example 30 rows for a 30 day period, creating null data for dates where there are no matching rows.

I wish to avoid using PHP to figure this out, so I thought I’d ask here for advice on how to maybe accomplish this with a smart query.

QUERY:

SELECT count(ID) AS views, date AS view_date
FROM logging.views 
WHERE date < DATE_SUB(NOW(),INTERVAL 30 DAY) 
GROUP BY DAY(date)";

Example below only gives me 7 rows, obviously because there are only entries matching 7 different days in the selected 30 DAY interval. I wish I could get 30 rows representing all days in the set interval.

OUTPUT:

Array
(
    [0] => Array
        (
            [date] => 2010-09-01 22:04:19
            [views] => 20
        )

    [1] => Array
        (
            [date] => 2010-08-13 00:15:54
            [views] => 4
        )

    [2] => Array
        (
            [date] => 2010-08-24 17:46:53
            [views] => 2
        )

    [3] => Array
        (
            [date] => 2010-08-25 07:18:11
            [views] => 35
        )

    [4] => Array
        (
            [date] => 2010-08-26 08:02:47
            [views] => 12
        )

    [5] => Array
        (
            [date] => 2010-08-27 09:56:05
            [views] => 29
        )

    [6] => Array
        (
            [date] => 2010-08-28 12:46:17
            [views] => 30
        )

)
  • 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-17T02:26:33+00:00Added an answer on May 17, 2026 at 2:26 am

    The common solution for this in SQL is to create a temp table with those 30 days, and LEFT OUTER JOIN it to your views table.

    SELECT COUNT(v.ID) AS views, d.date AS view_date
    FROM logging.alldays d
    LEFT OUTER JOIN logging.views v ON d.date = v.date
    WHERE d.date < DATE_SUB(NOW(),INTERVAL 30 DAY) 
    GROUP BY d.date
    

    Or make it a non-temp table and pre-populate it with all days for the next 100 years. That’s only 36525 rows, which is easy for an RDBMS.


    But what if I’d wanted to query by the hour, or minute?

    Then probably the temp table approach would be easier.

    Basically, SQL is good at joining sets of data to other sets of data — not joining data to thin air. So you have to make a set of data somewhere in order to join against it.

    Some brands of SQL database have built-in functions to generate an ephemeral data set representing some useful range of values. For instance, PostgreSQL has generate_series(). Unfortunately, MySQL doesn’t offer this type of function.

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

Sidebar

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.