SQL28 计算用户8月每天的练题数量 描述 题目:现在运营想要计算出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 t.a as day,count(t.device_id) as question_cnt from (select day(date) as a,device_id from question_practice_detail where date like '2021-08%') t group by t.a; NOW(),用于获取当前日期和时间函数 2024-03-16 10:58:06 select Now() CURDATE(),用于获取当前日期,同CURRENT_DATE() 2024-03-16 CURTIME(),用于获取当前时间,同CURRENT_TIME() 10:58:06 DATE()函数,用于提取时间字段的日期 2024-03-16 YEAR()函数,用于提取时间字段的年,,返回的格式为"YYYY" MONTH()函数,用于提取时间字段的月,返回的格式为"mm",若月份前有0会忽略 DAY()函数,用于提取时间字段的日期,,返回的格式为"dd",若日期前有0会忽略 TIME()函数,用于提取时间字段的时间,,返回的格式为"HH:ii:ss" HOUR()函数,用于提取时间字段的小时,,返回的格式为"HH",若小时前有0会忽略,如08则返回8 MINUTE()函数,用于提取时间字段的分钟,返回的格式为"ii",若分钟前有0会忽略,如08则返回8 SECOND()函数,用于提取时间字段的秒,返回的格式为"ss",若秒前有0会忽略,如08则返回8 UNIX_TIMESTAMP()函数,获取当前时间戳,还可以将时间字段转换成时间戳 WEEK()函数 获取日期对应的第几周 QUARTER() 获取日期对应的季度 DAYOFYEAR() 获取当天一日为这一年的第几天 DAYNAME() 获取对应周几 DAYOFMONTH() 获取日期是当月的第几天 TIME_TO_SEC() 获取时间对应的秒 ADDDATE('2023-05-18 10:58:59',2) 日期加N天
计算两个时间差
SELECT DATEDIFF('2023-05-31 10:58:59','2023-05-18 10:58:59')
日期减去N天
SELECT SUBDATE('2023-05-18 10:58:59',2)
--获取当前日期在本周的周一
select subdate(curdate(),date_format(curdate(),'%w')-1) ;
--获取当前日期在本周的周日
select subdate(curdate(),date_format(curdate(),'%w')-7);
返回:2023-05-21
drop table if exists `user_profile`; drop table if exists `question_practice_detail`; drop table if exists `question_detail`; CREATE TABLE `user_profile` ( `id` int NOT NULL, `device_id` int NOT NULL, `gender` varchar(14) NOT NULL, `age` int , `university` varchar(32) NOT NULL, `gpa` float, `active_days_within_30` int , `question_cnt` int , `answer_cnt` int ); 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 ); CREATE TABLE `question_detail` ( `id` int NOT NULL, `question_id`int NOT NULL, `difficult_level` varchar(32) NOT NULL ); INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学',3.4,7,2,12); INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0,15,5,25); INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2,12,3,30); INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6,5,1,2); INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8,20,15,70); INSERT INTO user_profile VALUES(6,2131,'male',28,'山东大学',3.3,15,7,13); INSERT INTO user_profile VALUES(7,4321,'male',28,'复旦大学',3.6,9,6,52); 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'); INSERT INTO question_detail VALUES(1,111,'hard'); INSERT INTO question_detail VALUES(2,112,'medium'); INSERT INTO question_detail VALUES(3,113,'easy'); INSERT INTO question_detail VALUES(4,115,'easy'); INSERT INTO question_detail VALUES(5,116,'medium'); INSERT INTO question_detail VALUES(6,117,'easy');
标签:INSERT,INTO,question,detail,牛客,练题,VALUES,2021,Mysql From: https://www.cnblogs.com/hellotoworld/p/18077488