Home >Database >Mysql Tutorial >Mysql usage tips: view-like subquery

Mysql usage tips: view-like subquery

藏色散人
藏色散人forward
2020-04-20 11:46:482474browse

The search description information includes the category name and the number of movies corresponding to robot movies, and the number of movies corresponding to the category is also required >= 5

Recommendation: "mysql video tutorial

The film table is a movie table, the category table is a movie classification table, and the film_category table is an intermediate table between the movie table and the movie classification table (a many-to-many intermediate table)

film Table

Mysql usage tips: view-like subquery##

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 table

Mysql usage tips: view-like subquery

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

film_category table

Mysql usage tips: view-like subquery

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

The above is the detailed content of Mysql usage tips: view-like subquery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete