Mysql query today, yesterday, last 7 days, last 30 days, this month, last month data, mysql January

Source: Internet
Author: User

Mysql query today, yesterday, last 7 days, last 30 days, this month, last month data, mysql January

Recently, the data record query function for the current month was used in the project. The original idea was to construct a period of time in the logical business for query. It was quite troublesome to write SQL statements. So I searched the internet to see if there is a simple method. Sure enough, the network resources are very powerful. Next we will use my project table to record the SQL statements for mysql query today, yesterday, last 7 days, last 30 days, and last month.

There is an ad_proTrack_t table. The time field of the tracing product is crt_time.

Query today's information records:

select * from ad_proTrack_t  where to_days(`crt_time`) = to_days(now());
Query yesterday's information records:
select * from ad_proTrack_t where to_days(now()) – to_days(`crt_time`) <= 1;
Query Information records in the last seven days:
select * from ad_proTrack_t where date_sub(curdate(), INTERVAL 7 DAY) <= date(`crt_time`);
Query the information records of the last 30 days:
select * from ad_proTrack_t  where date_sub(curdate(), INTERVAL 30 DAY) <= date(`crt_time`);
Query Information records for this month:
select * from ad_proTrack_t  where date_format(`crt_time`, ‘%Y%m') = date_format(curdate() , ‘%Y%m');
Query the information records of the last month:

select * from ad_proTrack_t where period_diff(date_format(now() , ‘%Y%m') , date_format(`crt_time`, ‘%Y%m')) =1;


In ASPNET, how do I query the records of today, yesterday, last 7 days, and last month? (Access database)

Select * from tb_user where datediff ('D', log_time, now) = 0
Select * from tb_user where datediff ('D', log_time, now) = 1
Select * from tb_user where datediff ('D' log _ time, now) = 7
Select * from tb_user where datediff ('D' log _ time, now) = 30

How does MYSQL query data within a week (last 7 days )?

Select * from wap_content where week (created_at) = week (now)

If you want to strictly require a year, you can

Query for one day:

Select * from table where to_days (column_time) = to_days (now ());
Select * from table where date (column_time) = curdate ();

Query for one week:

Select * from table where DATE_SUB (CURDATE (), INTERVAL 7 DAY) <= date (column_time );

Query for one month:

Select * from table where DATE_SUB (CURDATE (), INTERVAL 1 MONTH) <= date (column_time );

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.