Mysql by year, quarter, month, week, day SQL statistics query

Source: Internet
Author: User

Recently encountered a problem: the development-time table does not have a time stamp that is used directly for the day month field, but it is counted by days, months, and years

This without program processing directly with MySQL can handle:
MySQL does not have a specialized processing function for UNIX timestamps, so if you encounter time groupings and you are using an integer UNIX timestamp, then only the other date types that are converted to MySQL!

From_unixtim () converts a Unix timestamp to a datetime date type!

Querying data for the year
From Blog_article
WHERE year (From_unixtime (blogcreatetime)) = year (Curdate ())

Second, query quarterly data
Number of quarters with query data included
SELECT ArticleID, Quarter (from_unixtime (' Blogcreatetime '))
From ' Blog_article '
Other previous sections: Querying data for the quarter
From Blog_article
WHERE Quarter (From_unixtime (blogcreatetime)) = Quarter (Curdate ())

Third, query monthly data
Monthly Statistics (MySQL)
SELECT * FROM booking where month (booking_time) =

Month (Curdate ()) and year (booking_time) = year (Curdate ())

Weekly Stats (MySQL)

SELECT * FROM spf_booking where month (booking_time) =

Month (Curdate ()) and week (booking_time) = Week (Curdate ())

Iv. time period

N Days of recording

WHERE To_days (now ())-To_days (Time field) <= N

Record of the day

Where date (Time field) =date (now ())
Where To_days (Time field) = To_days (now ());

Query one week:
SELECT * FROM table where Date_sub (Curdate (), INTERVAL 7 day) <= DATE (column_time);

Query one months:
SELECT * FROM table where Date_sub (Curdate (), INTERVAL INTERVAL 1 MONTH) <= DATE (column_time);

Query ' 06-03 ' to ' 07-08 ' for all birthdays during this time period:

Select * from user Where

Date_format (Birthday, '%m-%d ') >= ' 06-03 ' and date_format (birthday, '%m-%d ')

<= ' 07-08 ';

Statistics quarterly data, table Time field: Savetime
Group BY Concat (Date_format (savetime, '%Y '), Floor ((Date_format (savetime, '%m ') +2)/3))
Select year (savetime) *10+ ((MONTH (Savetime)-1) DIV 3) +1,count (*)

From yourtable

Group by year (Savetime) *10+ ((MONTH (Savetime)-1) DIV 3) +1;

Five, group query

1. Group of the Year

2. Monthly Group

3, first group by year, and then by the monthly group

4. GROUP BY month
SELECT count (ArticleID), Date_format (From_unixtime (' blogcreatetime '), '%y%m ') sdate from ' Blog_article ' GROUP by sdate

Count (ArticleID) sdate
17 0901
11 0902
5 0903
6 0904
2 0905
1 0907
12 0908
6 0909
11 0910
3 0911

Mysql by year, quarter, month, week, day SQL statistics query

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.