MYSQL Query Day, this week, the last 7 days, this month, the last 30 days of statements

Source: Internet
Author: User
Tags mysql query

MYSQL Query Day, this week, the last 7 days, this month, the last 30 days of statementsOriginal April 13, 2017 16:40:38
    • Label:
    • 962
    • Edit
    • Delete

-The day

SELECT * from table name wherewhere= to_days (now ());
--This week

SELECT * FROM table name WHERE Yearweek (date_format (Time field, '%y-%m-%d ')) = Yearweek (now ());

--Last 7 days

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

--This month

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

--Last 30 days

SELECT * FROM table name WHERE date_sub (Curdate (), INTERVAL Day) <= date (Time field);

--Last month


SELECT  *fromWHERE'%y%m'%y%m' = 1

--This quarter
SELECT  * from table name  WHERE QUARTER (create_date)=QUARTER (now ());

Query last quarter data

SELECT * FROM table name WHERE       QUARTER (create_date)=1 QUARTER));

Query this year's data

SELECT * FROM table name WHEREYear (create_date)=  Year (now ());

Query last year data

SELECT * FROM table name WHEREYear (create_date)=  Year1));

query data for last week

    select  * from  table name WHERE        Span style= "COLOR: #0000ff" >  Yearweek (date_format (Submittime, " %y-%m-%d   ") =  Yearweek (now ()) - 1 ;

Querying data for the previous month

 SELECT * FROM table name WHEREDate_format (Submittime,'%y-%m')=Date_format (Date_sub (Curdate (), INTERVAL1 MONTH),'%y-%m') SELECT * FROM table name WHEREDate_format (Pudate,'%y%m')=Date_format (Curdate (),'%y%m') ;  SELECT * FROM table name WHEREWeekOfYear (From_unixtime (Pudate,'%y-%m-%d'))=WeekOfYear (now ()) SELECT * FROM table name WHERE MONTH(From_unixtime (Pudate,'%y-%m-%d'))= MONTH(now ()) SELECT * FROM table name WHERE  Year(From_unixtime (Pudate,'%y-%m-%d'))=  Year(Now ()) and MONTH(From_unixtime (Pudate,'%y-%m-%d'))= MONTH(now ()) SELECT * FROM table name WHEREPudatebetweenLast day of last month andThe first day of next month

Query data that is currently 6 months from now

SELECT * FROM table name WHEREbetween 6  Month and now ();





MYSQL Query Day, this week, the last 7 days, this month, the last 30 days of statements

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.