Use of MySQL time functions: query data for this week, next week, this month, and next month

Source: Internet
Author: User

From: http://www.myexception.cn/mysql/485349.html

Use of MySQL time functions: query data of this week, last week, this month, and last month]

/* Today */

Select * from table name where to_days (time field) = to_days (now ());

 

/* Yesterday */

Select * from table name where to_days (now ()-to_days (time field) = 1;

 

/* Last 7 days */

Select * from table name where date_sub (curdate (), interval 7 day) <= Date (time field );

 

/* Query data 6 months from the current time */

Select * from table name where time field between date_sub (now (), interval 6 month) and now ();

 

/* Query the data for the current week */

Select * from table name where yearweek (date_format (time field, '% Y-% m-% D') = yearweek (now ());

 

/* Query last week's data */

Select * from table name where yearweek (date_format (time field, '% Y-% m-% D') = yearweek (now ()-1;

 

/* Query the data of the current month */

Select * from table name where date_format (time field, '% Y-% m') = date_format (now (),' % Y-% m ');

 

/* Query the data of the previous month */

Select * from table name where date_format (time field, '% Y-% m') = date_format (date_sub (curdate (), interval 1 month ), '% Y-% m ');

 

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.