题目和要求
表: TVProgram
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| program_date | date |
| content_id | int |
| channel | varchar |
+---------------+---------+
(program_date, content_id) 是该表的主键(具有唯一值的列的组合)。
该表包含电视上的节目信息。
content_id 是电视一些频道上的节目的 id。
表: Content
+------------------+---------+
| Column Name | Type |
+------------------+---------+
| content_id | varchar |
| title | varchar |
| Kids_content | enum |
| content_type | varchar |
+------------------+---------+
content_id 是该表主键(具有唯一值的列)。
Kids_content 是枚举类型, 取值为('Y', 'N'), 其中:
'Y' 表示儿童适宜内容, 而'N'表示儿童不宜内容。
content_type 表示内容的类型, 比如电影, 电视剧等。
编写解决方案, 报告在 2020 年 6 月份播放的儿童适宜电影的去重电影名.
返回的结果表单 没有顺序要求 .
返回结果的格式如下例所示.
示例 1:
输入:
TVProgram 表:
+--------------------+--------------+-------------+
| program_date | content_id | channel |
+--------------------+--------------+-------------+
| 2020-06-10 08:00 | 1 | LC-Channel |
| 2020-05-11 12:00 | 2 | LC-Channel |
| 2020-05-12 12:00 | 3 | LC-Channel |
| 2020-05-13 14:00 | 4 | Disney Ch |
| 2020-06-18 14:00 | 4 | Disney Ch |
| 2020-07-15 16:00 | 5 | Disney Ch |
+--------------------+--------------+-------------+
Content 表:
+------------+----------------+---------------+---------------+
| content_id | title | Kids_content | content_type |
+------------+----------------+---------------+---------------+
| 1 | Leetcode Movie | N | Movies |
| 2 | Alg. for Kids | Y | Series |
| 3 | Database Sols | N | Series |
| 4 | Aladdin | Y | Movies |
| 5 | Cinderella | Y | Movies |
+------------+----------------+---------------+---------------+
输出:
+--------------+
| title |
+--------------+
| Aladdin |
+--------------+
解释:
"Leetcode Movie" 是儿童不宜的电影.
"Alg. for Kids" 不是电影.
"Database Sols" 不是电影
"Aladin" 是电影, 儿童适宜, 并且在 2020 年 6 月份播放.
"Cinderella" 不在 2020 年 6 月份播放.
1. 题目代码
Create table If Not Exists TVProgram ( program_date date , content_id int , channel varchar ( 30 ) ) ;
Create table If Not Exists Content ( content_id varchar ( 30 ) , title varchar ( 30 ) , Kids_content ENUM ( 'Y' , 'N' ) , content_type varchar ( 30 ) ) ;
Truncate table TVProgram;
insert into TVProgram ( program_date, content_id, channel) values ( '2020-06-10 08:00' , '1' , 'LC-Channel' ) ;
insert into TVProgram ( program_date, content_id, channel) values ( '2020-05-11 12:00' , '2' , 'LC-Channel' ) ;
insert into TVProgram ( program_date, content_id, channel) values ( '2020-05-12 12:00' , '3' , 'LC-Channel' ) ;
insert into TVProgram ( program_date, content_id, channel) values ( '2020-05-13 14:00' , '4' , 'Disney Ch' ) ;
insert into TVProgram ( program_date, content_id, channel) values ( '2020-06-18 14:00' , '4' , 'Disney Ch' ) ;
insert into TVProgram ( program_date, content_id, channel) values ( '2020-07-15 16:00' , '5' , 'Disney Ch' ) ;
Truncate table Content;
insert into Content ( content_id, title, Kids_content, content_type) values ( '1' , 'Leetcode Movie' , 'N' , 'Movies' ) ;
insert into Content ( content_id, title, Kids_content, content_type) values ( '2' , 'Alg. for Kids' , 'Y' , 'Series' ) ;
insert into Content ( content_id, title, Kids_content, content_type) values ( '3' , 'Database Sols' , 'N' , 'Series' ) ;
insert into Content ( content_id, title, Kids_content, content_type) values ( '4' , 'Aladdin' , 'Y' , 'Movies' ) ;
insert into Content ( content_id, title, Kids_content, content_type) values ( '5' , 'Cinderella' , 'Y' , 'Movies' ) ;
select distinct title
from Content c
left join tvprogram t on c. content_id= t. content_id
where program_date like '2020-06%' and Kids_content= 'Y' and content_type= 'Movies' ;
select distinct title
from Content
where Kids_content= 'Y' and content_type= 'Movies'
and content_id in ( select content_id from tvprogram where program_date like '2020-06%' ) ;
2. 解题思路流程图分析1(方法1:LEFT JOIN)
关联 content_id
筛选 program_date 如 '2020-06%'
过滤 Kids_content='Y' 和 content_type='Movies'
选择 distinct title
Content 表
TVProgram 表
过滤条件
结果集
3. 解题思路流程图分析2(方法2:子查询)
筛选 program_date 如 '2020-06%'
过滤 Kids_content='Y' 和 content_type='Movies'
选择 distinct title
TVProgram 表
提取 content_id
Content 表
进行 content_id 匹配
结果集
4. 解题思路流程图分析3(方法1:LEFT JOIN)
2023-10-01
2023-10-01
2023-10-02
2023-10-02
2023-10-03
2023-10-03
2023-10-04
2023-10-04
2023-10-05
关联 Content 和 TVProgram 表
筛选 program_date 满足 '2020-06%'
过滤 Kids_content='Y' 和 content_type='Movies'
选择 distinct title 作为结果集
LEFT JOIN 方法
方法1:LEFT JOIN 解题步骤
5. 解题思路流程图分析4(方法2:子查询)
2023-10-01
2023-10-01
2023-10-02
2023-10-02
2023-10-03
2023-10-03
2023-10-04
2023-10-04
2023-10-05
从 TVProgram 表中筛选 program_date 满足 '2020-06%'
提取符合条件的 content_id
从 Content 表中筛选 Kids_content='Y' 和 content_type='Movies'
匹配 content_id 并选择 distinct title 作为结果集
子查询方法
方法2:子查询 解题步骤
6. 难点分析
日期格式匹配 :在 WHERE
子句中使用 LIKE
进行日期范围的筛选,需要理解日期字符串的格式和匹配模式。表连接与子查询的选择 :决定使用 LEFT JOIN
还是子查询来实现同样的过滤条件,理解两者的性能和语义差异。数据类型一致性 :确保 content_id
在两个表中的数据类型一致,避免因为数据类型不匹配导致查询错误。去重操作 :使用 DISTINCT
关键字消除重复的标题,需要理解其对结果集的影响及性能考虑。
7. 答案代码
select distinct title
from Content c
left join tvprogram t on c. content_id= t. content_id
where program_date like '2020-06%' and Kids_content= 'Y' and content_type= 'Movies' ;
select distinct title
from Content
where Kids_content= 'Y' and content_type= 'Movies'
and content_id in ( select content_id from tvprogram where program_date like '2020-06%' ) ;
8. 关键总结
表的创建与数据插入 :掌握 CREATE TABLE IF NOT EXISTS
和 INSERT INTO
语句的使用。表连接(JOIN) :理解 LEFT JOIN
的工作原理及其在数据筛选中的应用。子查询 :掌握使用 IN
子句进行数据筛选的方法。条件筛选 :熟悉 WHERE
子句中多条件的组合使用,包括字符串匹配 (LIKE
) 和枚举类型过滤。数据去重 :使用 DISTINCT
关键字消除结果集中的重复记录。日期处理 :理解如何在 SQL 查询中处理和匹配日期格式。性能优化 :了解不同查询方法(如 JOIN 和子查询)的性能差异及其适用场景。