常用使用場景: 統計某個月(某個時間區間)內每一天的數據量
-
select
date_add(
curdate(),
interval(
cast(help_topic_id
as signed
integer) -
1)
day)
day
-
from mysql.help_topic
-
where help_topic_id <
day(
last_day(
curdate()))
-
order
by help_topic_id
延伸用法: 獲取一段時間內的每分鍾
-
set @stime =
str_to_date(
'2018-11-07 08:00',
'%Y-%m-%d %H:%i');
-
set @etime =
str_to_date(
'2018-11-07 08:10',
'%Y-%m-%d %H:%i');
-
-
select
date_add(@stime,
interval (
cast(help_topic_id
as signed
integer) -
0)
minute)
minute
-
from mysql.help_topic
-
where help_topic_id <=
timestampdiff(
minute, @stime, @etime)
-
order
by help_topic_id
順便貼一下oracle的寫法
-
select trunc(
sysdate,
'MM') +
rownum -
1
day
-
from dual
-
connect
by
rownum <= to_number(to_char(
last_day(
sysdate),
'dd'))