MySQL Query SQL code for this week, last week, this month, and last month's data

Source: Internet
Author: User
Tags mysql query

Http://www.jb51.net/article/32277.htm


MySQL query a lot of ways, the following for you to introduce the MySQL query is to query this week, last week, this month, the last month's data, if you are interested in MySQL query, may wish to see/ * Today * /
select * FROM table name where To_days (Time field) = To_days (now ());
/* Yesterday */
select * FROM table name where To_days (now ())-to_days (Time field) = 1;
/* Nearly 7 days */
select * FROM table name where Date_sub (Curdate (), Interval 7 day) <= Date (Time field); querying data for the current week SELECT Name,submittime from the Enterprise WHERE Yearweek (Date_format (submittime, '%y-%m-%d ')) = Yearweek (now ());

query last week's data
SELECT Name,submittime from the Enterprise WHERE Yearweek (Date_format (submittime, '%y-%m-%d ')) = Yearweek (now ())-1;

querying data for the current month
Select Name,submittime from Enterprise where Date_format (Submittime, '%y-%m ') =date_format (now (), '%y-%m ')

query data that is currently 6 months from now
Select Name,submittime from Enterprise where Submittime between Date_sub (now (), Interval 6 month) and now ();

querying data for the previous month
Select Name,submittime from Enterprise where Date_format (Submittime, '%y-%m ') =date_format (Date_sub (Curdate (), INTERVAL 1 MONTH), '%y-%m ')

SELECT * from ' user ' where Date_format (pudate, '%y%m ') = Date_format (Curdate (), '%y%m ');

SELECT * from user where WeekOfYear (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]
Where pudate between the last day of last month
And the first day of next month

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

MySQL Query SQL code for this week, last week, this month, and last month's 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.