基于mysql的sakila数据库脚本分析

时间:2022-12-07 18:24:02

本例是基于mysql的sakila数据库脚本的复杂查询分析,大家可以去mysql官网上下载此脚本;也可以进入我的资源页进行下载

关系图如下:

基于mysql的sakila数据库脚本分析

下面是查询的案例:

1.查询某部电影的所属类别,语言

SELECT film.title AS '电影名称', category.name AS '种类名称', language.name AS '语言'  FROM film_category
LEFT JOIN film ON film_category.`film_id` = film.`film_id`
LEFT JOIN `category` ON film_category.category_id = category.category_id
LEFT JOIN LANGUAGE ON film.language_id = language.language_id
WHERE film.title = 'ACADEMY DINOSAUR';

2.查询某部电影的演员有哪些

SELECT film.title AS '电影名称', CONCAT(actor.last_name, CONCAT(" ", actor.first_name)) AS '参与演员'
FROM film
INNER JOIN film_actor
ON film.`film_id` = film_actor.`film_id`
INNER JOIN actor
ON film_actor.`actor_id` = actor.`actor_id`
WHERE film.title = 'ACADEMY DINOSAUR';

3.查询某部电影的演员人数

SELECT film.title AS '电影名称', COUNT(film_actor.`actor_id`) AS '参演人数' FROM film
INNER JOIN film_actor ON film.`film_id` = film_actor.`film_id`
WHERE film.title = 'ACADEMY DINOSAUR';

4.查询电影id,库存数

SELECT film.`film_id` AS '电影id', COUNT(inventory.`film_id`) AS '库存数'
FROM film, inventory
WHERE film.`film_id` = inventory.`film_id`
GROUP BY film.`film_id`;

5.查询:电影名,库存数

SELECT film.`title` AS '电影名称', COUNT(inventory.`film_id`) AS '库存数'
FROM film LEFT JOIN inventory ON film.`film_id` = inventory.`film_id`
GROUP BY film.`title`;

6.查询store对应的地址和负责人姓名

SELECT address.`address` AS 'store地址', CONCAT(staff.first_name, CONCAT(" ", staff.`last_name`)) AS '负责人姓名'
FROM store
INNER JOIN address ON store.`address_id` = address.`address_id`
INNER JOIN staff ON store.`manager_staff_id` = staff.`staff_id`;

7.查询staff姓名及其对应的地址

SELECT CONCAT(staff.first_name, CONCAT(" ", staff.`last_name`)) AS 'staff姓名', address.`address` AS '住址'
FROM staff LEFT JOIN address ON staff.`address_id` = address.`address_id`;

8.查询某个顾客的消费记录:顾客名、消费金额

SELECT CONCAT(customer.`first_name`, CONCAT(" ", customer.`last_name`)) AS '顾客名', COUNT(payment.`amount`) AS '消费金额'
FROM customer
LEFT JOIN rental ON customer.`customer_id` = rental.`customer_id`
LEFT JOIN payment ON rental.`rental_id` = payment.`rental_id`
WHERE customer.`customer_id` = 1;

9.查询某个员工的销售总额:员工名,销售总额

SELECT CONCAT(staff.first_name, CONCAT(" ", staff.`last_name`)) AS '员工姓名', COUNT(payment.`amount`) AS '销售总额'
FROM payment LEFT JOIN staff ON payment.`staff_id` = staff.`staff_id`
GROUP BY staff.`first_name`;

10.查询某个顾客借出的影片名

SELECT `title` AS '影片名' -- CONCAT(`first_name`, CONCAT(" ", `last_name`)) AS '顾客名',
FROM
(
SELECT DISTINCT(customer.`customer_id`), customer.`first_name`, customer.`last_name`, inventory.`film_id`
FROM customer
INNER JOIN store ON customer.`store_id` = store.`store_id`
INNER JOIN inventory ON inventory.`store_id` = store.`store_id` ) AS tt
INNER JOIN film ON film.`film_id` = tt.film_id
WHERE `customer_id` = 1;

11.查询某片子的受欢迎程度,根据欢迎程度来进行排序

SELECT title AS '电影名称', COUNT(title) AS '租借次数'
FROM
(
SELECT film.`title`
FROM film
LEFT JOIN inventory ON film.`film_id` = inventory.`film_id`
LEFT JOIN rental ON inventory.`inventory_id` = rental.`inventory_id` ) AS tt
GROUP BY title
ORDER BY 租借次数 DESC;

12.查询滞留最多的DVD:什么是滞留最多??? 是不是借出次数最少的???

其中重复的语句比较多,怎么解决 where和 from后面的子表怎么传递??

SELECT title, title_sum
FROM
(
SELECT title, COUNT(title) AS 'title_sum'
FROM
(
SELECT film.`title`
FROM film
LEFT JOIN inventory ON film.`film_id` = inventory.`film_id`
LEFT JOIN rental ON inventory.`inventory_id` = rental.`inventory_id`
) AS tt
GROUP BY title
) AS aa
WHERE title_sum IN
(
SELECT MIN(title_sum)
FROM
(
SELECT title, COUNT(title) AS 'title_sum'
FROM
(
SELECT film.`title`
FROM film
LEFT JOIN inventory ON film.`film_id` = inventory.`film_id`
LEFT JOIN rental ON inventory.`inventory_id` = rental.`inventory_id` ) AS tt
GROUP BY title
) AS aa
);

13.查询消费最高的客户,以及该客户的最喜欢借阅的片子类型

SELECT cus_fav.cus_id AS '顾客编号', cus_fav.cus_name AS '顾客姓名', cus_fav.cate_name AS '最爱的电影类型', MAX(cus_fav.cate_num) AS '租借次数'
FROM
(
SELECT cus.id AS 'cus_id', cus.name AS 'cus_name', category.`name` AS 'cate_name', COUNT(category.`name`) AS 'cate_num'
FROM
(
SELECT cus_py.cus_id AS 'id', cus_py.cus_name AS 'name', MAX(cus_py.total_amount) AS 'amount'
FROM
(
SELECT customer.`customer_id` AS 'cus_id', CONCAT(customer.`first_name`, CONCAT(" ", customer.`last_name`)) AS 'cus_name', COUNT(payment.`amount`) AS 'total_amount'
FROM customer LEFT JOIN payment ON customer.`customer_id` = payment.`customer_id`
GROUP BY cus_id
) AS cus_py
) AS cus
INNER JOIN rental ON rental.`customer_id` = cus.id
INNER JOIN inventory ON inventory.`inventory_id` = rental.`inventory_id`
INNER JOIN film ON film.`film_id` = inventory.`film_id`
INNER JOIN film_category ON film_category.`film_id` = film.`film_id`
INNER JOIN category ON category.`category_id` = film_category.`category_id`
GROUP BY cate_name
) AS cus_fav;

14.查询某部电影所属的类别,语言,演员数

SELECT film.`title` AS '电影名称', category.`name` AS '类别', language.`name` AS '语言', COUNT(actor.`actor_id`) AS '演员人数'
FROM film
LEFT JOIN film_category ON film.`film_id` = film_category.`film_id`
LEFT JOIN category ON category.`category_id` = film_category.`category_id`
LEFT JOIN LANGUAGE ON film.`language_id` = language.`language_id`
LEFT JOIN film_actor ON film.`film_id` = film_actor.`film_id`
LEFT JOIN actor ON film_actor.`actor_id` = actor.`actor_id`
WHERE film.`film_id` = 1;

15.查询某个顾客的消费记录;接待的员工名。消费金额

SELECT cus.customer_name AS '顾客名', cus.staff_name AS '员工名', COUNT(cus.amount) AS '消费额'
FROM
(
SELECT CONCAT(customer.`first_name`, CONCAT(" ", customer.`last_name`)) AS 'customer_name', CONCAT(staff.`first_name`, CONCAT(" ", staff.`last_name`)) AS 'staff_name', payment.`amount` AS 'amount'
FROM customer
INNER JOIN payment ON customer.`customer_id` = payment.`customer_id`
INNER JOIN staff ON payment.`staff_id` = staff.`staff_id`
WHERE customer.`customer_id` = 1
) AS cus
GROUP BY cus.staff_name;

16.查询某个顾客的消费总数

SELECT CONCAT(customer.`first_name`, CONCAT(" ", customer.`last_name`)) AS '顾客名', SUM(payment.`amount`) AS '消费金额'
FROM customer
LEFT JOIN rental ON customer.`customer_id` = rental.`customer_id`
LEFT JOIN payment ON rental.`rental_id` = payment.`rental_id`
WHERE customer.`customer_id` = 1;