表: 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 表示内容的类型, 比如电影, 电视剧等.
问题
写一个 SQL 语句, 报告在 2020 年 6 月份播放的儿童适宜电影的去重电影名.
返回的结果表单没有顺序要求.
示例
查询结果的格式如下例所示.
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 |
Result 表:
title |
---|
Aladdin |
“Leetcode Movie” 是儿童不宜的电影.
“Alg. for Kids” 不是电影.
“Database Sols” 不是电影
“Alladin” 是电影, 儿童适宜, 并且在 2020 年 6 月份播放.
“Cinderella” 不在 2020 年 6 月份播放.
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/friendly-movies-streamed-last-month
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解答
法一
select distinct title
from tvprogram t right join content c
on t.content_id = c.content_id
where kids_content='Y' and content_type = 'Movies' and program_date like '2020-06%'
法二
select title from content
where kids_content = "Y" and content_type = "Movies"
and content_id in
(select content_id from tvprogram where month(program_date)='6' and year(program_date)='2020')
知识点
2020年6月的mysql表达方式
- program_date like '2020-06%
- year(program_date)=‘2020’ and month(program_date)=‘6’
- program_date bteween ‘2020-06-01’ and ‘2020-06-30’
- left(program_date,7)=‘6’
- date_formate(program_date,’%Y-%m’)=‘2020-06’
- DATEDIFF(‘2020-06-01’, program_date) BETWEEN 0 AND 29
- program_date regexp ‘^2020-06’ #正则表达式