描述
题目:现在运营想要计算出2021年8月每天用户练习题目的数量,请取出相应数据。
示例:question_practice_detail
id | device_id | question_id | result | date |
1 | 2138 | 111 | wrong | 2021-05-03 |
2 | 3214 | 112 | wrong | 2021-05-09 |
3 | 3214 | 113 | wrong | 2021-06-15 |
4 | 6543 | 111 | right | 2021-08-13 |
5 | 2315 | 115 | right | 2021-08-13 |
6 | 2315 | 116 | right | 2021-08-14 |
7 | 2315 | 117 | wrong | 2021-08-15 |
…… |
根据示例,你的查询应返回以下结果:
day | question_cnt |
13 | 5 |
14 | 2 |
15 | 3 |
16 | 1 |
18 | 1 |
具体代码实现:
SELECT DAY(date) AS day,COUNT(question_id) AS question_cnt
FROM question_practice_detail
WHERE YEAR(date)=2021 AND MONTH(date)=08
GROUP BY day
解题思路:
题目只有一个限定条件: 2021年8月每天;要实现这个条件限制的方法有很多,例如:
用 year/month 函数的 year(date)=2021 and month(date)=8
还可以用 date_format 函数的date_format(date, "%Y-%m")="202108"
每天:group by 按day分组;
总数:count()函数实现;