MySQL query sql about dates

Source: Internet
Author: User

---query today's records

SELECT * from WMACC. ' Cash_trade_record ' WHERE to_days (create_date) = To_days (now ());

SELECT * from WMACC. ' Cash_trade_record ' WHERE date_format (create_date, '%y%m%d ') = Date_format (Curdate (), '%y%m%d ');

---query yesterday's record

SELECT * from WMACC. ' Cash_trade_record ' WHERE to_days (create_date) = To_days (now ())-1;

---track records for the last 7 days

SELECT * from WMACC. ' Cash_trade_record ' WHERE date_sub (now (), INTERVAL 7 day) <= Create_date;

---query this week's records

SELECT * from WMACC. ' Cash_trade_record ' WHERE yearweek (date_format (create_date, '%y%m%d ')) = Yearweek (now ());

---Query last week's records

SELECT * from WMACC. ' Cash_trade_record ' WHERE yearweek (date_format (create_date, '%y%m%d ')) = Yearweek (now ())-1;

---query this month's records

SELECT * from WMACC. ' Cash_trade_record ' WHERE date_format (create_date, '%y%m ') = Date_format (Curdate (), '%y%m ');

---Query last month's records

SELECT * from WMACC. ' Cash_trade_record ' WHERE Period_diff (Date_format (now (), '%y%m '), Date_format (create_date, '%y%m ') ) = 1;

SELECT * from WMACC. ' Cash_trade_record ' WHERE date_format (create_date, '%y%m ') = Date_format (Date_sub (now (), INTERVAL 1 m onth), '%y%m ');

---Check the records for this quarter

SELECT * from WMACC. ' Cash_trade_record ' WHERE QUARTER (create_date) = QUARTER (Curdate ());

---Query last quarter's records

SELECT * from WMACC. ' Cash_trade_record ' WHERE QUARTER (create_date) = QUARTER (Date_sub (now (), INTERVAL 1 QUARTER));

---Check records for this year

SELECT * from WMACC. ' Cash_trade_record ' WHERE year (create_date) = year (now ());

---Querying records for previous year

SELECT * from WMACC. ' Cash_trade_record ' WHERE year (create_date) =year (Date_sub (now (), INTERVAL 1 year));



This article is from the "flyfish" blog, make sure to keep this source http://9381188.blog.51cto.com/9371188/1856961

MySQL query sql about dates

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.