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 );