SQL for date query related operations in MySQL database

Source: Internet
Author: User

Sometimes we have to count the data of the date specified in the database, such as the usual, today, yesterday, tomorrow, this week, last week, this month, the last month's data, as well as the date of the addition and subtraction, except we can use Strtotime to calculate also can refer to the following method. SQL minus the specified number of days

SELECT * FROM table name where Date_format (Time field, "%y-%m-%d") = Date_add (input time, INTERVAL-1 day);

/** If you need to add a specified number of days-1 to the specified number of days */

TodaySELECT * FROM table name where To_days (Time field name) =to_days (now ());

Yesterday

SELECT * from ' table name ' where To_days (now ()) –to_days (Time field name) = 1;

7 days

SELECT * from ' table name ' where Date_sub (Curdate (), INTERVAL 7 day) <= Date (Time field name);

/*datediff (now (), From_unixtime (' Time field name ')) = 7; Time field Storage Type is timestamp */

Nearly 30 days

SELECT * FROM table name where Date_sub (Curdate (), INTERVAL Day) <= Date (Time field name);

Month

SELECT * FROM table name where Date_format (Time field name, '%y%m ' = Date_format (Curdate (), '%y%m ';

querying data for the current week  

SELECT Name,submittime from the Enterprise WHERE Yearweek (Date_format (submittime, '%y-%m-%d ')) = Yearweek (now ());

Query last week's data

SELECT Name,submittime from the Enterprise WHERE Yearweek (Date_format (submittime, '%y-%m-%d ')) = Yearweek (now ())-1;

Querying data for the current month

Select Name,submittime from Enterprise where Date_format (Submittime, '%y-%m ') =date_format (now (), '%y-%m ')

Query data that is currently 6 months from now

Select Name,submittime from Enterprise where Submittime between Date_sub (now (), Interval 6 month) and now ();

Querying data for the previous month

Select Name,submittime from Enterprise where Date_format (Submittime, '%y-%m ') =date_format (Date_sub (Curdate (), INTER VAL 1 MONTH), '%y-%m ')

SELECT * from ' user ' where Date_format (pudate, '%y%m ') = Date_format (Curdate (), '%y%m ');

Because it didn't work so often, leave a note ~ ~ ~

SQL for date query related operations in MySQL database

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.