一 修复串列了的记录

  1. drop table if exists examination_info,exam_record;
  2. CREATE TABLE examination_info (
  3. id int PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID',
  4. exam_id int UNIQUE NOT NULL COMMENT '试卷ID',
  5. tag varchar(32) COMMENT '类别标签',
  6. difficulty varchar(8) COMMENT '难度',
  7. duration int NOT NULL COMMENT '时长',
  8. release_time datetime COMMENT '发布时间'
  9. )CHARACTER SET utf8 COLLATE utf8_general_ci;
  10. INSERT INTO examination_info(exam_id,tag,difficulty,duration,release_time) VALUES
  11. (9001, '算法', 'hard', 60, '2020-01-01 10:00:00'),
  12. (9002, '算法', 'hard', 80, '2020-01-01 10:00:00'),
  13. (9003, 'SQL', 'medium', 70, '2020-01-01 10:00:00'),
  14. (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

    细节问题:

  • 表头重命名:as

    完整代码:

    1. SELECT exam_id,
    2. substring_index(tag, ',', 1) AS tag,
    3. substring_index(substring_index(tag, ',', 2), ',', -1) AS difficulty,
    4. CAST( substring_index(tag, ',', -1) AS DECIMAL ) AS duration
    5. FROM examination_info
    6. WHERE 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’);

  1. <a name="kCjqS"></a>
  2. ### 明确题意:
  3. 输出昵称字符数大于10的用户信息,对于字符数大于13的用户昵称输出前10个字符然后加上三个点号:『...』
  4. <a name="fx58o"></a>
  5. ### 问题分解:
  6. - 筛选昵称字符数大于10的用户:WHERE CHAR_LENGTH(nick_name) > 10
  7. - 对字符数大于13的用户昵称做处理:IF(CHAR_LENGTH(nick_name) > 13,
  8. - 前10个字符加上三个点号:CONCAT(SUBSTR(nick_name, 1, 10), '...')
  9. <a name="LoiZQ"></a>
  10. ### 细节问题:
  11. - 表头重命名:as
  12. <a name="Zbr9Y"></a>
  13. ### 完整代码:
  14. ```java
  15. SELECT uid,
  16. IF(CHAR_LENGTH(nick_name) > 13,
  17. CONCAT(SUBSTR(nick_name, 1, 10), '...'),
  18. nick_name
  19. ) AS nick_name
  20. FROM user_info
  21. WHERE CHAR_LENGTH(nick_name) > 10;

三 大小写混乱时的筛选统计

  1. drop table if exists examination_info,exam_record;
  2. CREATE TABLE examination_info (
  3. id int PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID',
  4. exam_id int UNIQUE NOT NULL COMMENT '试卷ID',
  5. tag varchar(32) COMMENT '类别标签',
  6. difficulty varchar(8) COMMENT '难度',
  7. duration int NOT NULL COMMENT '时长',
  8. release_time datetime COMMENT '发布时间'
  9. )CHARACTER SET utf8 COLLATE utf8_bin;
  10. CREATE TABLE exam_record (
  11. id int PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID',
  12. uid int NOT NULL COMMENT '用户ID',
  13. exam_id int NOT NULL COMMENT '试卷ID',
  14. start_time datetime NOT NULL COMMENT '开始时间',
  15. submit_time datetime COMMENT '提交时间',
  16. score tinyint COMMENT '得分'
  17. )CHARACTER SET utf8 COLLATE utf8_general_ci;
  18. INSERT INTO examination_info(exam_id,tag,difficulty,duration,release_time) VALUES
  19. (9001, '算法', 'hard', 60, '2020-01-01 10:00:00'),
  20. (9002, 'C++', 'hard', 80, '2020-01-01 10:00:00'),
  21. (9003, 'c++', 'hard', 80, '2020-01-01 10:00:00'),
  22. (9004, 'sql', 'medium', 70, '2020-01-01 10:00:00'),
  23. (9005, 'C++', 'hard', 80, '2020-01-01 10:00:00'),
  24. (9006, 'C++', 'hard', 80, '2020-01-01 10:00:00'),
  25. (9007, 'C++', 'hard', 80, '2020-01-01 10:00:00'),
  26. (9008, 'SQL', 'medium', 70, '2020-01-01 10:00:00'),
  27. (9009, 'SQL', 'medium', 70, '2020-01-01 10:00:00'),
  28. (9010, 'SQL', 'medium', 70, '2020-01-01 10:00:00');
  29. INSERT INTO exam_record(uid,exam_id,start_time,submit_time,score) VALUES
  30. (1001, 9001, '2020-01-01 09:01:01', '2020-01-01 09:21:59', 80),
  31. (1002, 9003, '2020-01-20 10:01:01', '2020-01-20 10:10:01', 81),
  32. (1002, 9002, '2020-01-01 12:11:01', '2020-01-01 12:31:01', 83),
  33. (1003, 9002, '2020-01-01 19:01:01', '2020-01-01 19:30:01', 75),
  34. (1004, 9002, '2020-01-01 12:01:01', '2020-01-01 12:11:01', 60),
  35. (1005, 9002, '2020-01-01 12:01:01', '2020-01-01 12:41:01', 90),
  36. (1006, 9001, '2020-01-02 19:01:01', '2020-01-02 19:32:00', 20),
  37. (1007, 9003, '2020-01-02 19:01:01', '2020-01-02 19:40:01', 89),
  38. (1008, 9004, '2020-01-02 12:01:01', '2020-01-02 12:20:01', 99),
  39. (1008, 9001, '2020-01-02 12:01:01', '2020-01-02 12:31:01', 98),
  40. (1009, 9002, '2020-01-02 12:01:01', '2020-01-02 12:43:01', 81),
  41. (1010, 9002, '2020-01-02 12:11:01', null, null),
  42. (1011, 9001, '2020-01-02 10:01:01', '2020-01-02 10:31:01', 89);

题目主要信息:

  • 试卷的类别tag可能出现大小写混乱的情况,请先筛选出试卷作答数小于3的类别tag,统计将其转换为大写后对应的原本试卷作答数
  • 如果转换后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项的作答数作为输出。
    1. select a.tag as tag,
    2. b.answer_cnt as answer_cnt
    3. from(
    4. select tag,
    5. count(start_time) as answer_cnt
    6. from exam_record e_r join examination_info e_i
    7. on e_r.exam_id = e_i.exam_id
    8. group by tag
    9. ) a join(
    10. select tag,
    11. count(start_time) as answer_cnt
    12. from exam_record e_r join examination_info e_i
    13. on e_r.exam_id = e_i.exam_id
    14. group by tag
    15. ) b
    16. on a.tag != b.tag
    17. and upper(a.tag) = b.tag
    18. and 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. 1. 先对trans表中每个商品的购买数量的求和。
  2. 2. 将trans表与goods表用商品id进行连接。
  3. 3. 利用where语句进行筛选,并用id进行排序。
  4. ```java
  5. select g.id, g.name, g.weight,a.total
  6. from goods g join (
  7. select t.goods_id, sum(t.count) as total
  8. from trans t
  9. group by t.goods_id
  10. ) a
  11. on g.id = a.goods_id
  12. where g.weight < 50
  13. and a.total > 20
  14. order by id

五 网易云音乐推荐(网易校招笔试真题)

  1. CREATE TABLE `follow` (
  2. `user_id` int(4) NOT NULL,
  3. `follower_id` int(4) NOT NULL,
  4. PRIMARY KEY (`user_id`,`follower_id`));
  5. CREATE TABLE `music_likes` (
  6. `user_id` int(4) NOT NULL,
  7. `music_id` int(4) NOT NULL,
  8. PRIMARY KEY (`user_id`,`music_id`));
  9. CREATE TABLE `music` (
  10. `id` int(4) NOT NULL,
  11. `music_name` varchar(32) NOT NULL,
  12. PRIMARY KEY (`id`));
  13. INSERT INTO follow VALUES(1,2);
  14. INSERT INTO follow VALUES(1,4);
  15. INSERT INTO follow VALUES(2,3);
  16. INSERT INTO music_likes VALUES(1,17);
  17. INSERT INTO music_likes VALUES(2,18);
  18. INSERT INTO music_likes VALUES(2,19);
  19. INSERT INTO music_likes VALUES(3,20);
  20. INSERT INTO music_likes VALUES(4,17);
  21. INSERT INTO music VALUES(17,'yueyawang');
  22. INSERT INTO music VALUES(18,'kong');
  23. INSERT INTO music VALUES(19,'MOM');
  24. INSERT INTO music VALUES(20,'Sold Out');

思路:复杂问题,可以拆解成子问题,然后合起来就行了
1、查询他已经喜欢的音乐id

  1. select distinct(ml.music_id)
  2. from music_likes ml
  3. where ml.user_id = 1

2、查询他关注哪些人

  1. select distinct(f.follower_id)
  2. from follow f
  3. where f.user_id = 1

3、查询被关注人喜欢哪些音乐,
根据条件筛选,用户id是被关注人,歌曲id不是已经喜欢的
左连接,得到歌曲名,根据歌曲id排序,得到结果

  1. select distinct(m.music_name)
  2. from music_likes ml1
  3. left join music m
  4. on ml1.music_id = m.id
  5. where ml1.user_id in ( select distinct(f.follower_id)
  6. from follow f
  7. where f.user_id = 1 )
  8. and ml1.music_id not in ( select distinct(ml.music_id)
  9. from music_likes ml
  10. where ml.user_id = 1 )
  11. order by m.id asc