Home > Database > Mysql Tutorial > body text

SQL Query Counters Daily, Monthly, Yearly and Total Web Visits

王林
Release: 2023-09-06 15:09:03
forward
945 people have browsed it

SQL 查询计数器每天、每月、每年和总计的 Web 访问量

Let us understand how to build a query to find the number of network accesses per day, month, year and total in MySQL:

Note:We assume that we have created a database named "DBNAME" and a table named "tableName".

Let us see the MySQL query that can be used to get the daily web visits, month, year and total -

Query

SELECT COUNT(DISTINCT ip)
FROM tableName
WHERE create_at >= LAST_DAY(NOW()) + INTERVAL 1 DAY - INTERVAL 1 MONTH
AND create_at < LAST_DAY(NOW()) + INTERVAL 1 DAY
Copy after login

The above query starts from the current month and Search upto and Until to search for a range of DATETIME values ​​excluding the beginning of the next month.

Next, the composite covering index (create_at, ip) has been created. The above query will give the number of network visits per day, month, and year.

MySQL can scan the index range it needs.

Note: The above query also works for TIMESTAMP data.

The above is the detailed content of SQL Query Counters Daily, Monthly, Yearly and Total Web Visits. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template