【场景】:时间函数
【分类】:时间函数、年月、日
分析思路
难点:
1.如何得到2021年8月
2.如何得到日
题解中写出了4种得到20221年8月和日的方法
- 使用year、month、day
- 使用date_format
- 使用substring
- 使用like
扩展
求解代码
方法一:
使用year、month、day
select
day(date) as day,
count(device_id) as question_cnt
from question_practice_detail
where month(date) = 8 and year(date) = 2021
group by day
方法二
使用date_format
select
date_format(date,'%d') as day,
count(device_id) as question_cnt
from question_practice_detail
where date_format(date,'%Y%m') = '202108'
group by day
方法三
使用substring
select
substring(date,9,10) as day,
count(device_id) as question_cnt
from question_practice_detail
where substring(date,1,7) = '2021-08'
group by day
方法四
使用like
select
day(date) as day,
count(device_id) as question_cnt
from question_practice_detail
where date like '2021-08%'
group by day