首页 > 试题广场 >

计算用户8月每天的练题数量

[编程题]计算用户8月每天的练题数量
  • 热度指数:338258 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
题目:现在运营想要计算出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
8 3214 112 wrong 2021-05-09
9 3214 113 wrong 2021-08-15
10 6543 111 right 2021-08-13
11 2315 115 right 2021-08-13
12 2315 116 right 2021-08-14
13 2315 117 wrong 2021-08-15
14 3214 112 wrong 2021-08-16
15 3214 113 wrong 2021-08-18
16 6543 111 right 2021-08-13


根据示例,你的查询应返回以下结果:
day question_cnt
13 5
14 2
15 3
16 1
18 1

示例1

输入

drop table if  exists `question_practice_detail`;
CREATE TABLE `question_practice_detail` (
`id` int NOT NULL,
`device_id` int NOT NULL,
`question_id`int NOT NULL,
`result` varchar(32) NOT NULL,
`date` date NOT NULL
);
INSERT INTO question_practice_detail VALUES(1,2138,111,'wrong','2021-05-03');
INSERT INTO question_practice_detail VALUES(2,3214,112,'wrong','2021-05-09');
INSERT INTO question_practice_detail VALUES(3,3214,113,'wrong','2021-06-15');
INSERT INTO question_practice_detail VALUES(4,6543,111,'right','2021-08-13');
INSERT INTO question_practice_detail VALUES(5,2315,115,'right','2021-08-13');
INSERT INTO question_practice_detail VALUES(6,2315,116,'right','2021-08-14');
INSERT INTO question_practice_detail VALUES(7,2315,117,'wrong','2021-08-15');
INSERT INTO question_practice_detail VALUES(8,3214,112,'wrong','2021-05-09');
INSERT INTO question_practice_detail VALUES(9,3214,113,'wrong','2021-08-15');
INSERT INTO question_practice_detail VALUES(10,6543,111,'right','2021-08-13');
INSERT INTO question_practice_detail VALUES(11,2315,115,'right','2021-08-13');
INSERT INTO question_practice_detail VALUES(12,2315,116,'right','2021-08-14');
INSERT INTO question_practice_detail VALUES(13,2315,117,'wrong','2021-08-15');
INSERT INTO question_practice_detail VALUES(14,3214,112,'wrong','2021-08-16');
INSERT INTO question_practice_detail VALUES(15,3214,113,'wrong','2021-08-18');
INSERT INTO question_practice_detail VALUES(16,6543,111,'right','2021-08-13');

输出

day|question_cnt
13|5
14|2
15|3
16|1
18|1
select
    substring(b.date, 9, 2) as day,
    count(b.question_id) as question_cnt
from
    question_practice_detail b
where
    substring(b.date, 1, 7) = '2021-08'
group by
    day
order by
    day asc;

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;

select     day(b.date) day,     count(b.question_id) question_cnt from     question_practice_detail b where     month(b.date) = 8 and year(b.date) = 2021 group by     day order by     day asc;

发表于 2025-12-22 17:57:27 回复(0)
这个代码哪里错了啊看半天都看不出来,一直报错
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;

发表于 2025-11-17 19:11:06 回复(0)
select
    day(date) as day,
    count(question_id) as question_cnt
from
    question_practice_detail
where
    month(date) = 8
group by
    day(date)
发表于 2025-11-10 09:17:05 回复(0)
-- 取出特定时间段的数据:2021年8月内所有数据
-- 总结:每天练了多少题

select day(date) as day
    , count(1) as question_cnt
from
    question_practice_detail
where
    year(date) = 2021 and month(date) = 8
group by
    day(date)
;
发表于 2025-10-29 11:56:08 回复(0)
不知道日期函数:
select substring(date,9,2) as day,
count(date) as question_cnt 
from question_practice_detail 
where date like "2021-08-%" group by date;
发表于 2025-08-29 11:37:01 回复(0)
select substr(date,9,2) day,
count(device_id) question_cnt
from question_practice_detail
where substr(date,1,7)='2021-08'
group by day
之前喜欢用day、month等函数去筛选年月日,现在发现用suubstr函数更简单,而且在多个场合通用。
发表于 2025-08-08 16:16:35 回复(0)
为什么我看评论区里全都是用字符串进行筛选的,我都自我怀疑日期不能比较大小了
select
    day(date) as day,
    count(question_id) as question_cnt
from
    question_practice_detail
where
    date > '2021-07-31'
    and date < "2021-09-01"
group by
    day

发表于 2025-07-23 10:40:55 回复(1)
select right(date,2) as day, count(question_id) as question_cnt
from question_practice_detail
where left(date,7)='2021-08'
group by day
发表于 2025-07-16 16:17:16 回复(0)
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(date)

发表于 2025-07-15 13:27:18 回复(0)
-- 给你们一个错误代码但是结果是对的
select 
    day(date) as day,count(question_id) as question_cnt
from
    question_practice_detail
group by 
    day

发表于 2025-06-30 23:14:56 回复(0)
select
day(date) 'day',count(question_id) question_cnt
from
question_practice_detail
# where YEAR(date)=2021 and month(date) = 8
where date_format(date,'%Y%m') = '202108'
group by day(date)
;
直接用year和month来筛选也可以
用format筛选也行
发表于 2025-06-28 20:26:07 回复(0)
select 
    extract(day from date) as day,
    count(question_id) as question_cnt

from question_practice_detail
where extract(month from date) = '8'
group by extract(day from date) 

发表于 2025-06-20 21:08:47 回复(0)
select
    day(date) as day,
    count(*) as question_cnt
from question_practice_detail
where
    year(date)='2021'
    and month(date)='8'
group by day
发表于 2025-06-13 17:02:36 回复(0)
SELECT
    DAY(date) AS day,
    COUNT(*) AS question_cnt
FROM (SELECT date
        FROM question_practice_detail
        WHERE MONTH(date) = '08'
    ) AS 08_date
GROUP BY DAY(date)
发表于 2025-06-13 10:27:41 回复(0)
select day(date) as day,count(question_id) question_cnt
from question_practice_detail
where year(date)=2021 and month(date)=08
group by day;
发表于 2025-06-07 09:31:52 回复(0)
SELECT 
    RIGHT(date, 2) AS day,
    COUNT(device_id) AS question_cnt
FROM question_practice_detail
WHERE date LIKE '2021-08%'
GROUP BY date
怎么又跟大家不一样啊哈哈。。
发表于 2025-06-02 18:22:20 回复(0)
为什么不能排序?
发表于 2025-05-29 10:40:59 回复(0)
select substr(date,9,2) as day,count(question_id) question_cnt
from question_practice_detail
where date like '2021-08%'
group by day
发表于 2025-05-27 15:22:35 回复(0)