MySQL Query day, this week, this month, the last one months of data

Source: Internet
Author: User
Tags mysql query

Today

Select *  from where = To_days (now ());

Yesterday

SELECT *  from WHERE - <= 1

Nearly 7 days

SELECT *  from where 7  Day <= Date (Time field name)

Nearly 30 days

SELECT *  from where  -  Day <= Date (Time field name)

Month

SELECT *  from WHERE ' %y%m ' = ' %y%m ' )

Last month

SELECT *  from WHERE ' %y%m ' ' %y%m ' = 1

Query this quarterly data

Select *  from where QUARTER (create_date)=QUARTER (now ());

Query last quarter data

Select *  from where QUARTER (create_date)=1 QUARTER));

Query this year's data

Select *  from where  Year (create_date) =  Year (now ());

Query last year data

Select *  from where  Year (create_date) =  Year 1 year));

Querying data for the current week

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

Query last week's data

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

Querying data for the previous month

SelectName,submittime fromEnterprisewhereDate_format (Submittime,'%y-%m')=Date_format (Date_sub (Curdate (), INTERVAL1 MONTH),'%y-%m')Select *  from User whereDate_format (Pudate,'%y%m')=Date_format (Curdate (),'%y%m') ; Select *  from User whereWeekOfYear (From_unixtime (Pudate,'%y-%m-%d'))=WeekOfYear (now ())Select *  from User where MONTH(From_unixtime (Pudate,'%y-%m-%d'))= MONTH(now ())Select *  from User where  Year(From_unixtime (Pudate,'%y-%m-%d'))=  Year(Now ()) and MONTH(From_unixtime (Pudate,'%y-%m-%d'))= MONTH(now ())
Select * from User wherePudatebetweenLast day of last month andThe first day of next month

Querying data for the current month

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

Query data that is currently 6 months from now

Select  from where between 6 Month  and now ();

MySQL Query day, this week, this month, the last one months of data

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.