题目 : https://www.nowcoder.com/practice/e53e8797213748038a5faeb8563ba27a?tpId=341
数据
drop table if exists questions_pass_record;
drop table if exists user_info;
CREATE TABLE `questions_pass_record` (
`user_id` int NOT NULL,
`question_type` varchar(32) NOT NULL,
`device` varchar(14) NOT NULL,
`pass_count` int NOT NULL,
`date` date NOT NULL
);
CREATE TABLE `user_info` (
`user_id` int NOT NULL,
`graduation_year` int NOT NULL,
`register_time` datetime NOT NULL,
`gender` varchar(14),
`age` int,
`university` varchar(32) NOT NULL
);
INSERT INTO questions_pass_record VALUES(101, 'java', 'app', 2, '2020-03-01');
INSERT INTO questions_pass_record VALUES(102, 'sql', 'pc', 15,'2021-07-07');
INSERT INTO questions_pass_record VALUES(102, 'python', 'pc', 9, '2021-04-09');
INSERT INTO questions_pass_record VALUES(104, 'python', 'app', 3,'2022-03-17');
INSERT INTO questions_pass_record VALUES(105, 'sql', 'pc', 60, '2018-08-15');
INSERT INTO questions_pass_record VALUES(104, 'sql', 'pc', 20, '2019-05-15');
INSERT INTO user_info VALUES(101, 2022, '2021-03-01 11:22:33', 'male', 27, '北京大学');
INSERT INTO user_info VALUES(102, 2023, '2022-05-09 09:50:34', 'female', 31,'清华大学');
INSERT INTO user_info VALUES(103, 2021, '2022-03-09 15:10:50', 'male', null, '复旦大学');
INSERT INTO user_info VALUES(104, 2020, '2018-08-12 10:00:00', null, 23,'墨尔本大学');
INSERT INTO user_info VALUES(105, 2022, '2020-11-09 22:01:03', 'female', 26, '北京大学');
INSERT INTO user_info VALUES(210, 2022, '2022-03-09 01:07:09', 'male', 20, '上海交通大学');
需求
查询 2022 年毕业用户的刷题记录
查询结果 :
user_id|question_type|device|pass_count|date
101|java|app|2|2020-03-01
105|sql|pc|60|2018-08-15
解决
思路 :
- 2022 年 为限定值
- 查询刷题记录 , 要 2022年毕业的 , 就要 join 用户判断是否为 2022 年
select t1.user_id,
t1.question_type,
t1.device,
t1.pass_count,
t1.`date`
from questions_pass_record t1 join user_info t2
on t1.user_id = t2.user_id
where t2.graduation_year = '2022';