当前位置:Gxlcms > mysql > mysql使用技巧之行类视图子查询

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

时间:2021-07-01 10:21:17 帮助过:74人阅读

查找描述信息中包括robot的电影对应的分类名称以及电影数目,而且还需要该分类对应电影数量>=5部

推荐:《mysql视频教程》

film表为电影表,category表为电影分类表,film_category表为电影表与电影分类表的中间表(多对多的中间表)

film表

e891f450548126864c83481dfffb27f.png

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表

d18c82c05696fdf404d1eaebe9f07e2.png

CREATE TABLE category  (
category_id  tinyint(3)  NOT NULL ,
name  varchar(25) NOT NULL, `last_update` timestamp,
PRIMARY KEY ( category_id ));

film_category表

bd958a0a36204772284c54b8d88c9b5.png

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使用技巧之行类视图子查询的详细内容,更多请关注Gxlcms其它相关文章!

人气教程排行