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 Last 7 days SELECT * FROM table name where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(time field name) Last 30 days SELECT * FROM table name where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(time field name) this month SELECT * FROM table name WHERE DATE_FORMAT(time field name, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' ) Previous month SELECT * FROM table name WHERE PERIOD_DIFF( date_format( now(), '%Y%m' ) , date_format( time field name, '%Y%m' ) ) = 1 Query data for this quarter select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(now()); Query last quarter's data select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(DATE_SUB(now(),interval 1 QUARTER)); Query this year's data select * from `ht_invoice_information` where YEAR(create_date)=YEAR(NOW()); Query last year's data select * from `ht_invoice_information` where year(create_date)=year(date_sub(now(),interval 1 year)); Query the data for the current week SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now()); Query last week's data SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now())-1; Query last month's data 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 last day of previous month and first day of next month Query data for the current month select name,submittime from enterprise where date_format(submittime,'%Y-%m')=date_format(now(),'%Y-%m') Query data from 6 months ago select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now(); PS: Let’s see how to query the current day’s information in MySQL. It turned out that I was not very familiar with SQL query statements, and I had to look up everything I needed before I could use it. Fortunately, the Internet provided us with a lot of support. I used a statement again today, and I couldn't figure out how to solve it for a while. I looked it up online and it seemed like there was a solution. How could it be so simple? There are so many things that need to be accumulated. Let me record this simple question of mine today! It can be regarded as an accumulation: mysql query all information of the day: select * from test where year(regdate)=year(now()) and month(regdate)=month(now()) and day(regdate)=day(now()) This is a bit complicated, but there is a simple way to write it: select * from table where date(regdate) = curdate(); The date() function gets the date part, discards the time part, and then compares it with the current date. You may also be interested in:
|
<<: Detailed explanation of execution context and call stack in JavaScript
>>: Very practical Tomcat startup script implementation method
Table of contents Introduction to FTP, FTPS and S...
When developing a project, you will often encount...
Copy code The code is as follows: <iframe id=&...
Find the problem Recently, I found a problem at w...
Table of contents 1. Create a new project 2. Add ...
Table of contents 1. Introduction 2. select 2.1 Q...
How to add a loading animation every time I scrol...
reduce method is an array iteration method. Unlik...
1. Create a shell script vim backupdb.sh Create t...
1. MacVlan There are many solutions to achieve cr...
introduce RANGE partitioning is based on a given ...
question After Nginx reverse proxy, the Tomcat ap...
Project scenario: When running the Vue project, t...
Overview This article begins to introduce content...
Table of contents Overview Problem Description Ci...