Mysql is queried by SQL statement of the Year, quarter, month, week, or day.

Source: Internet
Author: User
The damn mysql does not provide a dedicated unix timestamp processing function. Therefore, if you encounter a time group and you use an integer unix timestamp, only other date types of mysql can be converted! FROM_UNIXTIM () converts unix timestamp to datetime type! 1. query data of the current year using the annual query SELECT * FROMblog_articleW

The damn mysql does not provide a dedicated unix timestamp processing function. Therefore, if you encounter a time group and you use an integer unix timestamp, only other date types of mysql can be converted! FROM_UNIXTIM () converts unix timestamp to datetime type! 1. query the data of the current year by querying the annual data SELECT * FROM blog_article W

The damn mysql does not provide a dedicated unix timestamp processing function. Therefore, if you encounter a time group and you use an integer unix timestamp, only other date types of mysql can be converted!
FROM_UNIXTIM () converts unix timestamp to datetime type!

1. Annual Query
Query data of the current year
SELECT *
FROM blog_article
WHERE year (FROM_UNIXTIME (BlogCreateTime) = year (curdate ())


Ii. query quarterly data
Query the number of accompanying quarters of data
SELECT ArticleId, quarter (FROM_UNIXTIME ('blogcreatetime '))
FROM 'blog _ article'
Others are the same as those in the previous section: query data for the current quarter
SELECT *
FROM blog_article
WHERE quarter (FROM_UNIXTIME (BlogCreateTime) = quarter (curdate ())



3. query monthly data
Statistics for this month (MySQL)
Select * from booking where month (booking_time) =

Month (curdate () and year (booking_time) = year (curdate ())

Weekly Statistics (MySQL)

Select * from spf_booking where month (booking_time) =

Month (curdate () and week (booking_time) = week (curdate ())


Iv. Time Period

Records within N days

WHERE TO_DAYS (NOW ()-TO_DAYS (time field) <= N


Today's records


Where date (time field) = date (now ())
Or
Where to_days (time field) = to_days (now ());

Query for one week:
Select * from table where DATE_SUB (CURDATE (), INTERVAL7DAY) <= date (column_time );

Query for one month:
Select * from table where DATE_SUB (CURDATE (), INTERVAL1MONTH) <= date (column_time );


Query all birthday members from '06-03' to '07-08:

Select * From user Where

DATE_FORMAT (birthday, '% m-% D')> = '06-03' and DATE_FORMAT (birthday, '% m-% D ')

<= '07-08 ';


Calculate the first quarter data. The table time field is savetime.
Group by concat (date_format (savetime, '% y'), FLOOR (date_format (savetime,' % m') + 2)/ 3))
Or
Select YEAR (savetime )* 10+ (MONTH (savetime )- 1) DIV 3) + 1, Count (*)

From yourTable

Group by YEAR (savetime )*10+ (MONTH (savetime )-1) DIV3) +1;

V. group query

1. Annual Group

2. Monthly grouping

3. Group by year and then by monthly

4. Group by month/year
SELECT count (ArticleId), date_format (FROM_UNIXTIME ('blogcreatetime'), '% y % m') sdate FROM 'blog _ article' group by sdate

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

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: info-contact@alibabacloud.com 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.