SQL面试题练习 —— 无效搜索

目录

  • [1 题目](#1 题目)
  • [2 建表语句](#2 建表语句)
  • [3 题解](#3 题解)

题目来源:百度。

1 题目

现有一份用户搜索日志,包含用户ID,时间,用户搜索内容。定义 无效搜索:如果用户下一次搜索内容中包含本次搜索内容,则认为本次搜索为无效搜索。请查询用户无效搜索记录。

样例数据

复制代码
+---------+---------------------+------------------------+
| user_id |     search_time      |     search_content      |
+---------+---------------------+------------------------+
|       1 | 2022-01-01 10:00:00 |          apple           |
|       1 | 2022-01-01 11:30:00 |    banana and apple      |
|       1 | 2022-01-01 12:45:00 |        fruit salad       |
|       1 | 2022-01-01 15:00:00 |         apple pie        |
|       1 | 2022-01-01 16:20:00 |   applesauce recipe      |
|       2 | 2022-01-01 10:00:00 |        cat food          |
|       2 | 2022-01-01 11:30:00 |   wet vs dry cat food    |
|       2 | 2022-01-01 12:45:00 | homemade cat food recipe |
|       2 | 2022-01-01 14:00:00 | cat food brands to avoid |
|       2 | 2022-01-01 16:20:00 | best cat food for i...   |
|       3 | 2022-01-01 10:00:00 |          book            |
|       3 | 2022-01-01 11:30:00 |  books like Harry Potter|
|       3 | 2022-01-01 13:00:00 |best selling books ...   |
|       3 | 2022-01-01 14:30:00 |   bookstores near me    |
|       3 | 2022-01-01 15:45:00 |    how to publish a book|
+---------+---------------------+------------------------+

2 建表语句

sql 复制代码
--建表语句
CREATE TABLE user_search_log (
  user_id STRING,
  search_time TIMESTAMP,
  search_content STRING
) STORED AS PARQUET;
--插入数据
INSERT INTO user_search_log
VALUES
  ('1', '2022-01-01 10:00:00', 'apple'),
  ('1', '2022-01-01 11:30:00', 'banana and apple'),
  ('1', '2022-01-01 12:45:00', 'fruit salad'),
  ('1', '2022-01-01 15:00:00', 'apple pie'),
  ('1', '2022-01-01 16:20:00', 'applesauce recipe'),
  ('2', '2022-01-01 10:00:00', 'cat food'),
  ('2', '2022-01-01 11:30:00', 'wet vs dry cat food'),
  ('2', '2022-01-01 12:45:00', 'homemade cat food recipe'),
  ('2', '2022-01-01 14:00:00', 'cat food brands to avoid'),
  ('2', '2022-01-01 16:20:00', 'best cat food for indoor cats'),
  ('3', '2022-01-01 10:00:00', 'book'),
  ('3', '2022-01-01 11:30:00', 'books like Harry Potter'),
  ('3', '2022-01-01 13:00:00', 'best selling books of all time'),
  ('3', '2022-01-01 14:30:00', 'bookstores near me'),
  ('3', '2022-01-01 15:45:00', 'how to publish a book');

3 题解

(1)查询出下一行数据,并把下一行搜索内容作为新字段放到本行

sql 复制代码
select user_id,
       search_time,
       search_content,
       lead(search_content) over (partition by user_id order by search_time asc) as next_search_content
from user_search_log

执行结果

复制代码
+----------+------------------------+---------------------------------+---------------------------------+
| user_id  |      search_time       |         search_content          |       next_search_content       |
+----------+------------------------+---------------------------------+---------------------------------+
| 1        | 2022-01-01 10:00:00.0  | apple                           | banana and apple                |
| 1        | 2022-01-01 11:30:00.0  | banana and apple                | fruit salad                     |
| 1        | 2022-01-01 12:45:00.0  | fruit salad                     | apple pie                       |
| 1        | 2022-01-01 15:00:00.0  | apple pie                       | applesauce recipe               |
| 1        | 2022-01-01 16:20:00.0  | applesauce recipe               | NULL                            |
| 2        | 2022-01-01 10:00:00.0  | cat food                        | wet vs dry cat food             |
| 2        | 2022-01-01 11:30:00.0  | wet vs dry cat food             | homemade cat food recipe        |
| 2        | 2022-01-01 12:45:00.0  | homemade cat food recipe        | cat food brands to avoid        |
| 2        | 2022-01-01 14:00:00.0  | cat food brands to avoid        | best cat food for indoor cats   |
| 2        | 2022-01-01 16:20:00.0  | best cat food for indoor cats   | NULL                            |
| 3        | 2022-01-01 10:00:00.0  | book                            | books like Harry Potter         |
| 3        | 2022-01-01 11:30:00.0  | books like Harry Potter         | best selling books of all time  |
| 3        | 2022-01-01 13:00:00.0  | best selling books of all time  | bookstores near me              |
| 3        | 2022-01-01 14:30:00.0  | bookstores near me              | how to publish a book           |
| 3        | 2022-01-01 15:45:00.0  | how to publish a book           | NULL                            |
+----------+------------------------+---------------------------------+---------------------------------+

在 MySQL 中,INSTR 函数的语法如下:INSTR(string, substring)string 是要搜索的字符串。
substring 是要查找的子字符串。INSTR 函数返回子字符串在字符串中首次出现的位置(从 1 开始)。如果未找到子字符串,则返回 0。

(2)比较搜索内容是否为下一次搜索内容的子字符串,给判断逻辑打标记(如果是返回1,否则返回0)

sql 复制代码
select user_id,
       search_time,
       search_content,
       lead(search_content) over (partition by user_id order by search_time asc)                                      as next_search_content,
       If(instr(lead(search_content) over (partition by user_id order by search_time asc), search_content) > 0, 1,
          0)                                                                                                          as flag
from user_search_log

执行结果

复制代码
+----------+------------------------+---------------------------------+---------------------------------+-------+
| user_id  |      search_time       |         search_content          |       next_search_content       | flag  |
+----------+------------------------+---------------------------------+---------------------------------+-------+
| 1        | 2022-01-01 10:00:00.0  | apple                           | banana and apple                | 1     |
| 1        | 2022-01-01 11:30:00.0  | banana and apple                | fruit salad                     | 0     |
| 1        | 2022-01-01 12:45:00.0  | fruit salad                     | apple pie                       | 0     |
| 1        | 2022-01-01 15:00:00.0  | apple pie                       | applesauce recipe               | 0     |
| 1        | 2022-01-01 16:20:00.0  | applesauce recipe               | NULL                            | 0     |
| 2        | 2022-01-01 10:00:00.0  | cat food                        | wet vs dry cat food             | 1     |
| 2        | 2022-01-01 11:30:00.0  | wet vs dry cat food             | homemade cat food recipe        | 0     |
| 2        | 2022-01-01 12:45:00.0  | homemade cat food recipe        | cat food brands to avoid        | 0     |
| 2        | 2022-01-01 14:00:00.0  | cat food brands to avoid        | best cat food for indoor cats   | 0     |
| 2        | 2022-01-01 16:20:00.0  | best cat food for indoor cats   | NULL                            | 0     |
| 3        | 2022-01-01 10:00:00.0  | book                            | books like Harry Potter         | 1     |
| 3        | 2022-01-01 11:30:00.0  | books like Harry Potter         | best selling books of all time  | 0     |
| 3        | 2022-01-01 13:00:00.0  | best selling books of all time  | bookstores near me              | 0     |
| 3        | 2022-01-01 14:30:00.0  | bookstores near me              | how to publish a book           | 0     |
| 3        | 2022-01-01 15:45:00.0  | how to publish a book           | NULL                            | 0     |
+----------+------------------------+---------------------------------+---------------------------------+-------+

(3)限制标签为1,查询出最后结果

sql 复制代码
select user_id,
       search_time,
       search_content
from (select user_id,
             search_time,
             search_content,
             if(instr(lead(search_content) over (partition by user_id order by search_time asc), search_content) > 0, 1,
                0) as flag
      from user_search_log) t
where flag = 1;

执行结果

复制代码
+----------+------------------------+-----------------+
| user_id  |      search_time       | search_content  |
+----------+------------------------+-----------------+
| 1        | 2022-01-01 10:00:00.0  | apple           |
| 2        | 2022-01-01 10:00:00.0  | cat food        |
| 3        | 2022-01-01 10:00:00.0  | book            |
+----------+------------------------+-----------------+
相关推荐
BTU_YC5 小时前
Neo4j查询计划完全指南:读懂数据库的“执行蓝图“
数据库·neo4j
非极限码农5 小时前
Neo4j图数据库上手指南
大数据·数据库·数据分析·neo4j
mit6.8245 小时前
[C# starter-kit] 命令/查询职责分离CQRS | MediatR |
java·数据库·c#
苏打水com6 小时前
数据库进阶实战:从性能优化到分布式架构的核心突破
数据库·后端
莫叫石榴姐6 小时前
SQL百题斩:从入门到精通,一站式解锁数据世界
大数据·数据仓库·sql·面试·职场和发展
shan~~7 小时前
linux达梦数据库操作
linux·数据库·chrome
武文斌777 小时前
项目学习总结:LVGL图形参数动态变化、开发板的GDB调试、sqlite3移植、MQTT协议、心跳包
linux·开发语言·网络·arm开发·数据库·嵌入式硬件·学习
CoderIsArt8 小时前
SQLite架构
数据库·sqlite
lixora8 小时前
银河麒麟高级服务器操作系统(ADM64 版)V10(SP1)搭建 Oracle 19c RAC
数据库
郝学胜-神的一滴8 小时前
使用Linux的read和write系统函数操作文件
linux·服务器·开发语言·数据库·c++·程序人生·软件工程