一 修复串列了的记录
drop table if exists examination_info,exam_record;CREATE TABLE examination_info (id int PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID',exam_id int UNIQUE NOT NULL COMMENT '试卷ID',tag varchar(32) COMMENT '类别标签',difficulty varchar(8) COMMENT '难度',duration int NOT NULL COMMENT '时长',release_time datetime COMMENT '发布时间')CHARACTER SET utf8 COLLATE utf8_general_ci;INSERT INTO examination_info(exam_id,tag,difficulty,duration,release_time) VALUES(9001, '算法', 'hard', 60, '2020-01-01 10:00:00'),(9002, '算法', 'hard', 80, '2020-01-01 10:00:00'),(9003, 'SQL', 'medium', 70, '2020-01-01 10:00:00'),(9004, '算法,medium,80','', 0, '2020-01-01 10:00:00');
明确题意:
部分记录的试题类别tag、难度、时长同时录入到了tag字段,找出这些录错了的记录,并拆分后按正确的列类型输出。
问题分解:
- 筛选出录错了的记录:WHERE tag LIKE ‘%,%’
- 提取tag,第一个逗号前的值:substring_index(tag, ‘,’, 1) AS tag
- 提取难度,第二个逗号前倒数第一个逗号后:substring_index(substring_index(tag, ‘,’, 2), ‘,’, -1) AS difficulty
提取时长,最后一个逗号后,并类型转换:CAST( substring_index(tag, ‘,’, -1) AS DECIMAL ) AS duration
细节问题:
-
完整代码:
SELECT exam_id,substring_index(tag, ',', 1) AS tag,substring_index(substring_index(tag, ',', 2), ',', -1) AS difficulty,CAST( substring_index(tag, ',', -1) AS DECIMAL ) AS durationFROM examination_infoWHERE tag LIKE '%,%';
二 对过长的昵称截取处理
```java
drop table if exists user_info;
CREATE TABLE user_info (
id int PRIMARY KEY AUTO_INCREMENT COMMENT ‘自增ID’,
uid int UNIQUE NOT NULL COMMENT ‘用户ID’,
nick_name varchar(64) COMMENT ‘昵称’,
achievement int COMMENT ‘成就值’,
level int COMMENT ‘用户等级’,
job varchar(32) COMMENT ‘职业方向’,
register_time datetime COMMENT ‘注册时间’
)CHARACTER SET utf8 COLLATE utf8_general_ci;
INSERT INTO user_info(uid,nick_name,achievement,level,job,register_time) VALUES
(1001, ‘牛客1’, 19, 0, ‘算法’, ‘2020-01-01 10:00:00’),
(1002, ‘牛客2号’, 1200, 3, ‘算法’, ‘2020-01-01 10:00:00’),
(1003, ‘牛客3号♂’, 22, 0, ‘算法’, ‘2020-01-01 10:00:00’),
(1004, ‘牛客4号’, 25, 0, ‘算法’, ‘2020-01-01 11:00:00’),
(1005, ‘牛客5678901234号’, 4000, 7, ‘算法’, ‘2020-01-01 10:00:00’),
(1006, ‘牛客67890123456789号’, 25, 0, ‘算法’, ‘2020-01-02 11:00:00’);
<a name="kCjqS"></a>### 明确题意:输出昵称字符数大于10的用户信息,对于字符数大于13的用户昵称输出前10个字符然后加上三个点号:『...』<a name="fx58o"></a>### 问题分解:- 筛选昵称字符数大于10的用户:WHERE CHAR_LENGTH(nick_name) > 10- 对字符数大于13的用户昵称做处理:IF(CHAR_LENGTH(nick_name) > 13,- 前10个字符加上三个点号:CONCAT(SUBSTR(nick_name, 1, 10), '...')<a name="LoiZQ"></a>### 细节问题:- 表头重命名:as<a name="Zbr9Y"></a>### 完整代码:```javaSELECT uid,IF(CHAR_LENGTH(nick_name) > 13,CONCAT(SUBSTR(nick_name, 1, 10), '...'),nick_name) AS nick_nameFROM user_infoWHERE CHAR_LENGTH(nick_name) > 10;
三 大小写混乱时的筛选统计
drop table if exists examination_info,exam_record;CREATE TABLE examination_info (id int PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID',exam_id int UNIQUE NOT NULL COMMENT '试卷ID',tag varchar(32) COMMENT '类别标签',difficulty varchar(8) COMMENT '难度',duration int NOT NULL COMMENT '时长',release_time datetime COMMENT '发布时间')CHARACTER SET utf8 COLLATE utf8_bin;CREATE TABLE exam_record (id int PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID',uid int NOT NULL COMMENT '用户ID',exam_id int NOT NULL COMMENT '试卷ID',start_time datetime NOT NULL COMMENT '开始时间',submit_time datetime COMMENT '提交时间',score tinyint COMMENT '得分')CHARACTER SET utf8 COLLATE utf8_general_ci;INSERT INTO examination_info(exam_id,tag,difficulty,duration,release_time) VALUES(9001, '算法', 'hard', 60, '2020-01-01 10:00:00'),(9002, 'C++', 'hard', 80, '2020-01-01 10:00:00'),(9003, 'c++', 'hard', 80, '2020-01-01 10:00:00'),(9004, 'sql', 'medium', 70, '2020-01-01 10:00:00'),(9005, 'C++', 'hard', 80, '2020-01-01 10:00:00'),(9006, 'C++', 'hard', 80, '2020-01-01 10:00:00'),(9007, 'C++', 'hard', 80, '2020-01-01 10:00:00'),(9008, 'SQL', 'medium', 70, '2020-01-01 10:00:00'),(9009, 'SQL', 'medium', 70, '2020-01-01 10:00:00'),(9010, 'SQL', 'medium', 70, '2020-01-01 10:00:00');INSERT INTO exam_record(uid,exam_id,start_time,submit_time,score) VALUES(1001, 9001, '2020-01-01 09:01:01', '2020-01-01 09:21:59', 80),(1002, 9003, '2020-01-20 10:01:01', '2020-01-20 10:10:01', 81),(1002, 9002, '2020-01-01 12:11:01', '2020-01-01 12:31:01', 83),(1003, 9002, '2020-01-01 19:01:01', '2020-01-01 19:30:01', 75),(1004, 9002, '2020-01-01 12:01:01', '2020-01-01 12:11:01', 60),(1005, 9002, '2020-01-01 12:01:01', '2020-01-01 12:41:01', 90),(1006, 9001, '2020-01-02 19:01:01', '2020-01-02 19:32:00', 20),(1007, 9003, '2020-01-02 19:01:01', '2020-01-02 19:40:01', 89),(1008, 9004, '2020-01-02 12:01:01', '2020-01-02 12:20:01', 99),(1008, 9001, '2020-01-02 12:01:01', '2020-01-02 12:31:01', 98),(1009, 9002, '2020-01-02 12:01:01', '2020-01-02 12:43:01', 81),(1010, 9002, '2020-01-02 12:11:01', null, null),(1011, 9001, '2020-01-02 10:01:01', '2020-01-02 10:31:01', 89);
题目主要信息:
- 试卷的类别tag可能出现大小写混乱的情况,请先筛选出试卷作答数小于3的类别tag,统计将其转换为大写后对应的原本试卷作答数
-
问题拆分:
选出每个标签的试卷对应的作答数:
- 以试卷标签tag作为分组。知识点:group by
- 标签信息与作答信息分布两个表中,依靠exam_id将表exam_record与表examination_info连接起来。知识点:join…on…
- 用每组的开始作答时间统计每组的作答数。知识点:count
- 筛选结果记为a
- 用上述相同的方式筛选出一个相同的表b
- 将表a和表b连接,连接项当且仅当表a中的标签不等于表b中的标签,但是表a中的标签转成大写以后等于表b中的标签,且表中该标签对应的作答数小于3. on a.tag != b.tag and upper(a.tag) = b.tag and a.answer_cnt < 3 知识点:join…on…、upper()
- 从连接后的表中选出表a项的tag及表b项的作答数作为输出。
select a.tag as tag,b.answer_cnt as answer_cntfrom(select tag,count(start_time) as answer_cntfrom exam_record e_r join examination_info e_ion e_r.exam_id = e_i.exam_idgroup by tag) a join(select tag,count(start_time) as answer_cntfrom exam_record e_r join examination_info e_ion e_r.exam_id = e_i.exam_idgroup by tag) bon a.tag != b.tagand upper(a.tag) = b.tagand a.answer_cnt < 3
四 商品交易(网易校招笔试真题)
```java
CREATE TABLE goods (
id int(11) NOT NULL,
name varchar(10) DEFAULT NULL,
weight int(11) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE trans (
id int(11) NOT NULL,
goods_id int(11) NOT NULL,
count int(11) NOT NULL,
PRIMARY KEY (id)
);
insert into goods values(1,’A1’,100);
insert into goods values(2,’A2’,20);
insert into goods values(3,’B3’,29);
insert into goods values(4,’T1’,60);
insert into goods values(5,’G2’,33);
insert into goods values(6,’C0’,55);
insert into trans values(1,3,10);
insert into trans values(2,1,44);
insert into trans values(3,6,9);
insert into trans values(4,1,2);
insert into trans values(5,2,65);
insert into trans values(6,5,23);
insert into trans values(7,3,20);
insert into trans values(8,2,16);
insert into trans values(9,4,5);
insert into trans values(10,1,3);
1. 先对trans表中每个商品的购买数量的求和。2. 将trans表与goods表用商品id进行连接。3. 利用where语句进行筛选,并用id进行排序。```javaselect g.id, g.name, g.weight,a.totalfrom goods g join (select t.goods_id, sum(t.count) as totalfrom trans tgroup by t.goods_id) aon g.id = a.goods_idwhere g.weight < 50and a.total > 20order by id
五 网易云音乐推荐(网易校招笔试真题)
CREATE TABLE `follow` (`user_id` int(4) NOT NULL,`follower_id` int(4) NOT NULL,PRIMARY KEY (`user_id`,`follower_id`));CREATE TABLE `music_likes` (`user_id` int(4) NOT NULL,`music_id` int(4) NOT NULL,PRIMARY KEY (`user_id`,`music_id`));CREATE TABLE `music` (`id` int(4) NOT NULL,`music_name` varchar(32) NOT NULL,PRIMARY KEY (`id`));INSERT INTO follow VALUES(1,2);INSERT INTO follow VALUES(1,4);INSERT INTO follow VALUES(2,3);INSERT INTO music_likes VALUES(1,17);INSERT INTO music_likes VALUES(2,18);INSERT INTO music_likes VALUES(2,19);INSERT INTO music_likes VALUES(3,20);INSERT INTO music_likes VALUES(4,17);INSERT INTO music VALUES(17,'yueyawang');INSERT INTO music VALUES(18,'kong');INSERT INTO music VALUES(19,'MOM');INSERT INTO music VALUES(20,'Sold Out');
思路:复杂问题,可以拆解成子问题,然后合起来就行了
1、查询他已经喜欢的音乐id
select distinct(ml.music_id)from music_likes mlwhere ml.user_id = 1
2、查询他关注哪些人
select distinct(f.follower_id)from follow fwhere f.user_id = 1
3、查询被关注人喜欢哪些音乐,
根据条件筛选,用户id是被关注人,歌曲id不是已经喜欢的
左连接,得到歌曲名,根据歌曲id排序,得到结果
select distinct(m.music_name)from music_likes ml1left join music mon ml1.music_id = m.idwhere ml1.user_id in ( select distinct(f.follower_id)from follow fwhere f.user_id = 1 )and ml1.music_id not in ( select distinct(ml.music_id)from music_likes mlwhere ml.user_id = 1 )order by m.id asc
