MySQL query today, yesterday, last week

Source: Internet
Author: User
Tags mysql query

Today
SELECT * FROM table name where To_days (Time field name) =to_days (now ());
Yesterday
SELECT * FROM table name WHERE to_days (now ())-To_days (Time field name) <= 1
7 days
SELECT * FROM table name where Date_sub (Curdate (), INTERVAL 7 day) <=date (Time field name)
Nearly 30 days
SELECT * FROM table name where Date_sub (Curdate (), INTERVAL Day) <=date (Time field name)
Month
SELECT * FROM table name WHERE date_format (Time field name, '%y%m ') = Date_format (Curdate (), '%y%m ')
Last month
SELECT * FROM table name WHERE Period_diff (Date_format (now (), '%y%m '), Date_format (Time field name, '%y%m ')) =1
#查询本季度数据
SELECT * from ' ht_invoice_information ' Wherequarter (create_date) =quarter (now ());
#查询上季度数据
SELECT * from ' ht_invoice_information ' Wherequarter (create_date) =quarter (Date_sub (now (), Interval 1QUARTER));
#查询本年数据
SELECT * from ' ht_invoice_information ' Whereyear (create_date) =year (now ());
#查询上年数据
SELECT * from ' ht_invoice_information ' Whereyear (create_date) =year (Date_sub (now (), Interval 1year));
Querying data for the current week
SELECT Name,submittime from Enterprise Whereyearweek (Date_format (submittime, '%y-%m-%d ')) =yearweek (now ());
Query last week's data
SELECT Name,submittime from Enterprise Whereyearweek (Date_format (submittime, '%y-%m-%d ')) =yearweek (now ())-1;
Querying data for the current month
Select Name,submittime fromenterprise wheredate_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 betweendate_sub (now (), Interval 6 month) Andnow ();
Querying data for the previous month
Select Name,submittime fromenterprise wheredate_format (submittime, '%y-%m ') =date_format (Date_sub (Curdate (), INTERVAL 1MONTH), '%y-%m ')
SELECT * from ' user ' where Date_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]
Where pudate between the last day of last month
And the first day of next month
where date (regdate) = Curdate ();
SELECT * FROM Test whereyear (regdate) =year (now ()) Andmonth (regdate) =month (now ()) Andday (regdate) =day (now ())
SELECT Date (c_instime), Curdate ()
From ' T_score '
WHERE 1
LIMIT 0, 30

MySQL query today, yesterday, last week

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.