sql語句獲取今天、昨天、近7天、本周、上周、本月、上月、半年數據
01 話說有一文章表article,存儲文章的添加文章的時間是add_time字段,該字段為int(5)類型的,現需要查詢今天添加的文章總數並且按照時間從大到小排序,則查詢語句如下:
02
03 1 select * from `article` where date_format(from_UNIXTIME(`add_time`),'%Y-%m-%d') = date_format(now(),'%Y-%m-%d');
04 或者:
05
06 1 select * from `article` where to_days(date_format(from_UNIXTIME(`add_time`),'%Y-%m-%d')) = to_days(now());
07 假設以上表的add_time字段的存儲類型是DATETIME類型或者TIMESTAMP類型,則查詢語句也可按如下寫法:
08
09 查詢今天的信息記錄:
10
11 1 select * from `article` where to_days(`add_time`) = to_days(now());
12 查詢昨天的信息記錄:
13
14 1 select * from `article` where to_days(now()) – to_days(`add_time`) <= 1;
15 查詢近7天的信息記錄:
16
17 1 select * from `article` where date_sub(curdate(), INTERVAL 7 DAY) <= date(`add_time`);
18 查詢近30天的信息記錄:
19
20 1 select * from `article` where date_sub(curdate(), INTERVAL 30 DAY) <= date(`add_time`);
21 查詢本月的信息記錄:
22
23 1 select * from `article` where date_format(`add_time`, ‘%Y%m') = date_format(curdate() , ‘%Y%m');
24 查詢上一月的信息記錄:
25
26 1 select * from `article` where period_diff(date_format(now() , ‘%Y%m') , date_format(`add_time`, ‘%Y%m')) =1;
27 對上面的SQL語句中的幾個函數做一下分析:
28
29 (1)to_days
30
31 就像它的名字一樣,它是將具體的某一個日期或時間字符串轉換到某一天所對應的unix時間戳,如:
32
33 01 mysql> select to_days('2010-11-22 14:39:51');
34 02 +--------------------------------+
35 03 | to_days('2010-11-22 14:39:51') |
36 04 +--------------------------------+
37 05 | 734463 |
38 06 +--------------------------------+
39 07
40 08 mysql> select to_days('2010-11-23 14:39:51');
41 09 +--------------------------------+
42 10 | to_days('2010-11-23 14:39:51') |
43 11 +--------------------------------+
44 12 | 734464 |
45 13 +--------------------------------+
46 可以看出22日與23日的差別就是,轉換之后的數增加了1,這個粒度的查詢是比較粗糙的,有時可能不能滿足我們的查詢要求,那么就需要使用細粒度的查詢方法str_to_date函數了,下面將分析這個函數的用法。
47
48 提醒:
49
50 (1)to_days() 不用於陽歷出現(1582)前的值,原因是當日歷改變時,遺失的日期不會被考慮在內。因此對於1582 年之前的日期(或許在其它地區為下一年 ), 該函數的結果實不可靠的。
51
52 (2)MySQL"日期和時間類型"中的規則是將日期中的二位數年份值轉化為四位。因此對於'1997-10-07'和'97-10-07'將被視為同樣的日期:
53
54 1 mysql> select to_days('1997-10-07'), to_days('97-10-07');
55 2
56 3 -> 729669, 729669
57 (2)str_to_date
58
59 這個函數可以把字符串時間完全的翻譯過來,如:
60
61 1 mysql> select str_to_date("2010-11-23 14:39:51",'%Y-%m-%d %H:%i:%s');
62 2
63 3 +--------------------------------------------------------+
64 4 | str_to_date("2010-11-23 14:39:51",'%Y-%m-%d %H:%i:%s') |
65 5 +--------------------------------------------------------+
66 6 | 2010-11-23 14:39:51 |
67 7 +--------------------------------------------------------+
68 具體案例操作如下:
69
70 1 select str_to_date(article.`add_time`,'%Y-%m-%d %H:%i:%s')
71 2 from article
72 3 where str_to_date(article.`add_time`,'%Y-%m-%d %H:%i:%s')>='2012-06-28 08:00:00' and str_to_date(article.`add_time`,'%Y-%m-%d %H:%i:%s')<='2012-06-28 09:59:59';
查詢
今天
select * from 表名 where to_days(時間字段名) = to_days(now());
昨天
SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) – TO_DAYS( 時間字段名) <= 1
7天
SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(時間字段名)
近30天
SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(時間字段名)
本月
SELECT * FROM 表名 WHERE DATE_FORMAT( 時間字段名, ‘%Y%m’ ) = DATE_FORMAT( CURDATE( ) , ‘%Y%m’ )
上一月
SELECT * FROM 表名 WHERE PERIOD_DIFF( date_format( now( ) , ‘%Y%m’ ) , date_format( 時間字段名, ‘%Y%m’ ) ) =1
同時,再附上 一個 mysql官方的相關document
#查詢本季度數據
select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(now());
#查詢上季度數據
select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(DATE_SUB(now(),interval 1 QUARTER));
#查詢本年數據
select * from `ht_invoice_information` where YEAR(create_date)=YEAR(NOW());
#查詢上年數據
select * from `ht_invoice_information` where year(create_date)=year(date_sub(now(),interval 1 year));
查詢當前這周的數據
SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now());
查詢上周的數據
SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now())-1;
查詢當前月份的數據
select name,submittime from enterprise where date_format(submittime,'%Y-%m')=date_format(now(),'%Y-%m')
查詢距離當前現在6個月的數據
select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();
查詢上個月的數據
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 上月最后一天
and 下月第一天
where date(regdate) = curdate();
select * from test where year(regdate)=year(now()) and month(regdate)=month(now()) and day(regdate)=day(now())
SELECT date( c_instime ) ,curdate( )
FROM `t_score`
WHERE 1
LIMIT 0 , 30
轉:https://www.cnblogs.com/xiaoqian1993/p/5260789.html