简体   繁体   中英

Best method views by date

I am trying to make a views system for my website.

I got a 770 rows of articles in my MySQL database and I'm trying to do a views system to know which article got the most views and when. (Today, 3 Days Ago, This week, This month).

I tried to do something like this in MySQL database:

Table: views

ID  articleID   date

But then I don't know how to sum it up? and If I will add column 'views' it won't help me because I have to change the dates all time.

This is the code for any of your view

CREATE VIEW purpose AS
SELECT 
    articleID, 
    COUNT(articleID) as view_count 
FROM views 
WHERE date_condition_depending_on_purpose 
GROUP BY articleID

PS index on articleID is vital.

All articles:

SELECT
    COUNT(*) AS number,
    articleID
FROM
    views
GROUP BY
    articleID
ORDER BY
    number DESC

2012 only:

SELECT
    COUNT(*) AS number,
    articleID
FROM
    views
WHERE
    YEAR(date) = 2012
GROUP BY
    articleID
ORDER BY
    number DESC

For a specific article for each month:

SELECT
    COUNT(*) AS number,
    MONTH(date) AS stats_month,
    YEAR(date) AS stats_year
FROM
    views
GROUP BY
    stats_month,
    stats_year
ORDER BY
    number DESC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM