MySQL SQL statement for querying data by date

Source: Internet
Author: User

Query data for 7 days ahead:

[HTML]View PlainCopyprint?
    1. SELECT * from data table where Date_sub (Curdate (), INTERVAL 7 day) <= the Time field name you want to judge
[HTML]View PlainCopy print?

Query data for 30 days ahead:

[HTML]View PlainCopyprint?
    1. SELECT * from data table where Date_sub (Curdate (), INTERVAL Day) <= The time field name you want to judge
[HTML]View PlainCopy print?

To query data between a certain date:

[HTML]View PlainCopyprint?
    1. SELECT * from data table where time field name between ' 2016-02-01 ' and ' 2016-02-05 '

Query data for 3 months ahead:

[HTML]View PlainCopy print?
    1. SELECT * from data table where time field name between Date_sub (now (), INTERVAL 3 MONTH) and now ()
[HTML]View PlainCopyprint?

Query data for one year ahead:

[HTML]View PlainCopyprint?
    1. SELECT * from data table where time field name between Date_sub (now (), INTERVAL 1 years) and now ()
[HTML]View PlainCopyprint?

Query this month's data

[HTML]View PlainCopyprint?
    1. SELECT * from data table where Date_format (Time field name, '%y-%m ') =date_format (now (), '%y-%m ')

Query last month's data

SELECT * from data table where Date_format (Time field name, '%y-%m ') =date_format (Date_sub (Curdate (), INTERVAL 1 MONTH), '%y-%m ')


Query this week's data

[HTML]View PlainCopyprint?
    1. SELECT * from data table where Yearweek (date_format (Time field name, '%y-%m-%d ')) = Yearweek (now ())

Query data for last week

[HTML]View PlainCopyprint?
    1. SELECT * from data table where Yearweek (Date_format (create_time, '%y-%m-%d ')) = Yearweek (now ())-1



I hope you find the blog has errors and I proposed that bloggers will be the first time to update the article.

Thank you.

MySQL SQL statement for querying data by date

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.