MySQL Query by date

Source: Internet
Author: User

SELECT * FROM table where to_days (dateline) = To_days (now ());
SELECT * FROM table where date (dateline) = Curdate ();

--Query yesterday records
SELECT * FROM table where to_days (dateline) = To_days (now ())-1;
SELECT * FROM table where date (dateline) = Curdate ()-1; --Today is the first day of the month to find the last day of the month record

--Query this week's records
SELECT * FROM table where Yearweek (Date_format (Dateline, '%y-%m-%d ')) = Yearweek (now ());
SELECT * FROM table where Date_sub (Curdate (), INTERVAL 7 day) <= DATE (Dateline); --7 days before the check

--Query last week records
SELECT * FROM table where Yearweek (Date_format (Dateline, '%y-%m-%d ')) = Yearweek (now ())-1;

--Check this month's record
SELECT * FROM table where Date_format (Dateline, '%y%m ') = Date_format (Curdate (), '%y%m ');
SELECT * FROM table where Date_sub (Curdate (), INTERVAL 1 MONTH) <= DATE (Dateline);--30 days before check

--Query last month records
SELECT * FROM table where Period_diff (Date_format (now (), '%y%m '), Date_format (Dateline, '%y%m ')) = 1;
SELECT * FROM table where Date_format (Dateline, '%y%m ') = Date_format (Curdate (), '%y%m ')-1;
SELECT * FROM table where Date_format (Dateline, '%y-%m ') =date_format (Date_sub (Curdate (), INTERVAL 1 MONTH), '%y-%m '); --60 days to the first 30 days before the check

MySQL Query by date

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.