MySQL query yesterday, today, 7 days, nearly 30 days, this month, last month data

Source: Internet
Author: User
Tags mysql query

When developing or counting, we may need to count the data in a table. For example: Look at today's new articles, calculate the number of new users this month, and so on. There are a lot of similar requirements, and it can be common to use the SQL technique to do this. I have used some of the SQL statements to summarize, I hope to provide you with some help, but also welcome comments or message to add.
#=============================================== #昨天 SELECT * FROM table name WHERE to_days (now ())-To_days (Time field name) <= 1 #今天 SELECT * FROM table name WHERE to_days (Time field name) = To_days (now ());
#7天 select * FROM table name where Date_sub (Curdate (), INTERVAL 7 day) <= Date (Time field name) #近30天 SELECT * from table name where date_sub (Curdate (), INTERVAL-day) <= Date (Time field name) #本月 SELECT * FROM table name WHERE date_format (Time field name, '%y%m ') = Date_format (C Urdate (), '%y%m ')
#上一月 SELECT * FROM table name WHERE Period_diff (Date_format (now (), '%y%m '), Date_format (Time field name, '%y%m ')) = 1 #=========   ================================================ #查询本季度数据 SELECT * FROM table name WHERE QUARTER (Time field name) =quarter (now ());   #查询上季度数据 SELECT * FROM table name WHERE QUARTER (Time field name) =quarter (Date_sub (now (), INTERVAL 1 QUARTER));   #查询本年数据 SELECT * FROM table name WHERE year (Time field name) =year (now ());    #查询上年数据 SELECT * FROM table name WHERE year (Time field name) =year (Date_sub (now (), INTERVAL 1)); #========================================================= #查询当前这周的数据 SELECT * from table name where Yearweek (date_sub (Time field name, '%y-%m-%d ') = Yearweek (now ());
#查询上周的数据 SELECT * FROM table name WHERE Yearweek (date_sub (Time field name, '%y-%m-%d ')) = Yearweek (now ())-1;
#查询当前月份的数据 SELECT * FROM table name WHERE date_sub (Time field name, '%y-%m ') =date_sub (now (), '%y-%m ')
#查询距离当前现在6个月的数据 SELECT * FROM table name WHERE Time field name between Date_sub (now (), INTERVAL 6 MONTH) Andnow ();
#查询上个月的数据 SELECT * FROM table name WHERE date_sub (Time field name, '%y-%m ') =date_sub (Date_sub (Curdate (), INTERVAL 1 MONTH), '%y-%m ') text Chapter synchronization released in Lang degree Cloud website, Portal: http://www.wolfbe.com/detail/201608/291.html

MySQL query yesterday, today, 7 days, nearly 30 days, this month, last month data

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: 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.