首页 > 试题广场 >

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

[编程题]计算用户8月每天的练题数量
  • 热度指数:293548 时间限制: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 
    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)
这题有问题啊,给的示例是按照日期排序的,但是提交的时候如果你写了排序就提交不了
发表于 2025-05-16 14:09:37 回复(0)

有大神能够解答下这个问题吗?

这段代码能通过自测,但没办法通过完整的测试,没办法提交。

# solution1:
select day(date) as day,count(question_id) as 	question_cnt
from question_practice_detail
where date like '2021-08%'
group by day
order by day # 加上这句就不能通过测试!



# solution2:
select day(date) as day,count(question_id) as 	question_cnt
from question_practice_detail
group by date
having date like '2021-08%'
order by day # 加上这句就不能通过测试!


发表于 2025-04-15 17:59:31 回复(1)
select
    case
        when date = '2021-8-13' then 13
        when date = '2021-8-14' then 14
        when date = '2021-8-15' then 15
        when date = '2021-8-16' then 16
        when date = '2021-8-17' then 17
        when date = '2021-8-18' then 18
    end as day,
    count(*) as question_cnt
from question_practice_detail
group by day
为什么不能这么做呢?这样做后结果会多出现一行none,怎样才能不出现none,还是说必须用day(date)呀?
发表于 2025-04-14 13:05:41 回复(0)
解题思路:
select date_format(date,'%d')as day,count(question_id) as question_cnt
from question_practice_detail
where date between '2021-08-01' and '2021-08-31' 
group by day;


发表于 2025-04-04 16:47:14 回复(0)
select day(date) day	
,count(question_id) question_cnt
from question_practice_detail
where date between '2021-08-01' and '2021-08-31'
group by date

发表于 2025-03-28 14:02:26 回复(0)
SELECT DAY(date) AS day, count(*) AS question_cnt
FROM question_practice_detail
WHERE date BETWEEN '2021-08-01' AND '2021-08-31'
GROUP BY date;

发表于 2025-03-19 23:09:26 回复(0)
SELECT EXTRACT(DAY FROM qp.date) AS day,
        count(*) AS question_cnt
FROM question_practice_detail AS qp
WHERE EXTRACT(MONTH FROM qp.date) = '08'
GROUP BY day;
发表于 2025-02-15 13:29:17 回复(0)
select day(date) day, count(question_id) question_cnt
from question_practice_detail
where date like '2021-08%'
group by day(date);

select day(date) day, count(distinct question_id) question_cnt
from question_practice_detail
where date like '2021-08%'
group by day(date);
为什么加了distinct就会报错
发表于 2025-02-07 17:35:36 回复(0)
select
  day(`date`) as `day`,
  count(*) as question_cnt
from question_practice_detail
# where date_format(`date`, '%Y-%m') = '2021-08'  -- Yes
where year(`date`) = 2021 and month(`date`) = 08
group by day(`date`)
发表于 2025-02-06 11:59:07 回复(0)