mysql使用技巧 行类视图子查询

2022-10-08,,,

查找描述信息中包括robot的电影对应的分类名称以及电影数目,而且还需要该分类对应电影数量>=5部
film表为电影表,category表为电影分类表,film_category表为电影表与电影分类表的中间表(多对多的中间表)

film表

字段 说明
film_id 电影id
title 电影名称
description 电影描述信息

create table if not exists film (
film_id smallint(5)  not null default '0',
title varchar(255) not null,
description text,
primary key (film_id));
                                                                                                                         
category表

字段 说明
category_id 电影分类id
name 电影分类名称
last_update 电影分类最后更新时间

create table category  (
category_id  tinyint(3)  not null ,
name  varchar(25) not null, `last_update` timestamp,
primary key ( category_id ));
                                                                                                                                     
film_category表

字段 说明
film_id 电影id
category_id 电影分类id
last_update 电影id和分类id对应关系的最后更新时间

create table film_category  (
film_id  smallint(5)  not null,
category_id  tinyint(3)  not null, `last_update` timestamp);

select c.name, count(f.film_id) as amount
from film as f, film_category as fc, category as c,
(select category_id from film_category group by category_id having count(category_id) >= 5) as cc
where f.description like '%robot%'
and f.film_id = fc.film_id
and fc.category_id = c.category_id
and c.category_id = cc.category_id

 

《mysql使用技巧 行类视图子查询.doc》

下载本文的Word格式文档,以方便收藏与打印。