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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:22:21+00:00 2026-06-16T23:22:21+00:00

I’m tracking articles views by Total, Today, Yesterday, Week, Month and Year, and I

  • 0

I’m tracking articles views by Total, Today, Yesterday, Week, Month and Year, and I want to sort them accordingly
I have a view table tracking each value, with the ID being the article:

MYSQL HIT TABLE

When a particular article gets a view, I increment a value to Total, Today, Week, Month, and Year.
So far so good..
but when tomorrow morning comes, what’s the best way to tally up?..

For example: I need to take Yesterday’s Today column and copy to Yesterday column, and if the Week has changed I need to delete all rows in Week (also with month and year accordingly) –

I realize I can make another table that keeps track of which day/wk/mnth/yr it is and compare it with PHP, then making the changes with PHP, but is there a better way that perhaps I’m unaware of? MYSQL caching, cronjobs or something?..

  • 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-16T23:22:22+00:00Added an answer on June 16, 2026 at 11:22 pm

    What happens if your site is down for a day? Who fixes the broken progression in your database? What if the update is missed on Sunday, Jan 31? All of your values will be off, and have fun fixing it.

    You should only be storing the view counts in one place, and manipulating that data to fit your needs.

    The table structure you should have:

    TABLE Articles
      ID INT PK
      ...
    
    TABLE Viewcount
      Date        DATE  PK
      Article_ID  INT   PK
      NumViews    INT
    

    To update the viewcount:

    INSERT INTO Viewcount (Date, Article_ID, NumViews)
      VALUES(TODAY(), 12345, 1)
      ON DUPLICATE KEY UPDATE NumViews=NumViews+1
    

    And to get your counts:

    SELECT ar.ID,
      today.numviews as 'today',
      yesterday.numviews as 'yesterday',
      thisweek.sum as 'week',
      thismonth.sum as 'month',
      thisyear.sum as 'year',
      alltime.sum as 'alltime'
    FROM Articles ar LEFT JOIN (
      SELECT Article_id, NumViews
      FROM Viewcount
      WHERE Date = TODAY()
    ) 'today'
    ON ar.ID = today.Article_ID
    LEFT JOIN (
      SELECT Article_id, NumViews
      FROM Viewcount
      WHERE Date = DATE_SUB(TODAY(), INTERVAL 1 DAY)
    ) 'yesterday'
    ON ar.ID = yesterday.Article_ID
    LEFT JOIN (
      SELECT Article_id, SUM(NumViews) 'sum'
      FROM Viewcount
      WHERE YEAR(Date) = YEAR()
        AND WEEK(Date) = WEEK()
      GROUP BY Article_ID
    ) 'thisweek'
    ON ar.ID = thisweek.Article_ID
    LEFT JOIN (
        SELECT Article_id, SUM(NumViews) 'sum'
      FROM Viewcount
      WHERE YEAR(Date) = YEAR()
        AND MONTH(Date) = MONTH()
      GROUP BY Article_ID
    ) 'thismonth'
    ON ar.ID = thismonth.Article_ID
    LEFT JOIN (
        SELECT Article_id, SUM(NumViews) 'sum'
      FROM Viewcount
      WHERE YEAR(Date) = YEAR()
      GROUP BY Article_ID
    ) 'thisyear'
    ON ar.ID = thisyear.Article_ID
    LEFT JOIN (
        SELECT Article_id, SUM(NumViews) 'sum'
      FROM Viewcount
      GROUP BY Article_ID
    ) 'alltime'
    ON ar.ID = alltime.Article_ID
    

    Or just do each query programmatically for the periods you would like to fetch.

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.