- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
SQL queries for counter web visits per day, month, year and totals
Let us understand how to form the query to find the number of web visits per day, per month, per year, and the total in MySQL:
Note: We assume we have created a database named ‘DBNAME’ and a table named ‘tableName’.
Let us see the MySQL query which can be used to get the web visits per day, month, year and totals −
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
The above query searches through a range of DATETIME values by beginning from the present month and searching upto, and until, but without including the beginning of the next month.
Next, a compound covering index on the (create_at, ip) is created. The above query will give the count of the web visits per day, per month, per year.
MySQL can scan the index range which it requires.
Note: The above query works fine for TIMESTAMP data too.
- Related Articles
- Format MySQL date and convert to year-month-day
- Convert day of year to day of month in Java
- Finding day of week from date (day, month, year) in JavaScript
- Combine three strings (day, month, year) and calculate next date PHP?
- Filter the records of current day, month and year in MySQL?
- How to get current day, month and year in Java 8?
- Create date from day, month, year fields in MySQL?
- Fetch month, day, year, etc. from ISODate in MongoDB?
- How to convert year, month, and day of the month into a complete date in R?
- How to get a Date from year, month and day in Java?
- How to combine year, month, and day column in an R data frame?
- Extract the Day / Month / Year from a Timestamp in PHP MySQL?
- How to compare Year, Month and Day in a MySQL query and display matching records
- How to get day of month, day of year and day of week in android using offset date time API class?
- MongoDB query to search for Month and Day only?
