When developing or counting, we may need to count the data in a table. For example: Look at today's new articles, calculate the number of new users this month, and so on. There are a lot of similar requirements, and it can be common to use the SQL technique to do this. I have used some of the SQL statements to summarize, I hope to provide you with some help, but also welcome comments or message to add.
#=============================================== #昨天 SELECT * FROM table name WHERE to_days (now ())-To_days (Time field name) <= 1 #今天 SELECT * FROM table name WHERE to_days (Time field name) = To_days (now ());
#7天 select * FROM table name where Date_sub (Curdate (), INTERVAL 7 day) <= Date (Time field name) #近30天 SELECT * from table name where date_sub (Curdate (), INTERVAL-day) <= Date (Time field name) #本月 SELECT * FROM table name WHERE date_format (Time field name, '%y%m ') = Date_format (C Urdate (), '%y%m ')
#上一月 SELECT * FROM table name WHERE Period_diff (Date_format (now (), '%y%m '), Date_format (Time field name, '%y%m ')) = 1 #========= ================================================ #查询本季度数据 SELECT * FROM table name WHERE QUARTER (Time field name) =quarter (now ()); #查询上季度数据 SELECT * FROM table name WHERE QUARTER (Time field name) =quarter (Date_sub (now (), INTERVAL 1 QUARTER)); #查询本年数据 SELECT * FROM table name WHERE year (Time field name) =year (now ()); #查询上年数据 SELECT * FROM table name WHERE year (Time field name) =year (Date_sub (now (), INTERVAL 1)); #========================================================= #查询当前这周的数据 SELECT * from table name where Yearweek (date_sub (Time field name, '%y-%m-%d ') = Yearweek (now ());
#查询上周的数据 SELECT * FROM table name WHERE Yearweek (date_sub (Time field name, '%y-%m-%d ')) = Yearweek (now ())-1;
#查询当前月份的数据 SELECT * FROM table name WHERE date_sub (Time field name, '%y-%m ') =date_sub (now (), '%y-%m ')
#查询距离当前现在6个月的数据 SELECT * FROM table name WHERE Time field name between Date_sub (now (), INTERVAL 6 MONTH) Andnow ();
#查询上个月的数据 SELECT * FROM table name WHERE date_sub (Time field name, '%y-%m ') =date_sub (Date_sub (Curdate (), INTERVAL 1 MONTH), '%y-%m ') text Chapter synchronization released in Lang degree Cloud website, Portal: http://www.wolfbe.com/detail/201608/291.html
MySQL query yesterday, today, 7 days, nearly 30 days, this month, last month data