Mysql asks for time between yesterday and last month's Today's time function

Source: Internet
Author: User

Problem:

MySQL table in a column create_time, type datetime (YYYY-MM-DD HH:MM:SS), want to get data from today to yesterday last month.

SELECT * FROM table name where date_format (create_time, '%y-%m-%d ') between date_sub (Curdate (), Interval 1 MONTH) and Date_sub (Curdate (), Interval 1 day)

After careful study, MySQL has a lot of time types.

/* 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;
/* Nearly 7 days */
SELECT * FROM table name where Date_sub (Curdate (), Interval 7 day) <= Date (Time field);
/* Query data from current 6 months */
SELECT * from table name where Time field between Date_sub (now (), Interval 6 month) and now ();
/* Query the current week's data */
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 data for the current month */
SELECT * FROM table name where Date_format (Time field, '%y-%m ') =date_format (now (), '%y-%m ');
/* Query last month's data */
SELECT * FROM table name where Date_format (Time field, '%y-%m ') =date_format (Date_sub (Curdate (), Interval 1 month), '%y-%m ');

Mysql asks for time between yesterday and last month's Today's time function

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.