Advertisement
a_khuzina

SQL_mistake 2_1

Jan 22nd, 2022 (edited)
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- весь запрос
  2. SELECT cat.name AS name_category,
  3.        COUNT(DISTINCT m.film_id) AS total_films
  4. FROM movie AS m
  5. LEFT JOIN film_category AS fc ON m.film_id=fc.film_id
  6. LEFT JOIN category AS cat ON fc.category_id=cat.category_id
  7. LEFT JOIN film_actor AS fa ON fc.film_id=fa.film_id
  8. WHERE fa.actor_id IN (SELECT fa1.actor_id
  9.                      FROM film_actor AS fa1
  10.                      LEFT JOIN movie AS m1 ON m1.film_id=fa1.film_id
  11.                      WHERE m1.release_year > 2013
  12.                      GROUP BY fa1.actor_id
  13.                      HAVING fa1.actor_id > 7)
  14. GROUP BY name_category
  15. ORDER BY total_films DESC;
  16.  
  17. -- отдельно подзапрос с WHERE с разными положениями таблиц в LEFT JOIN
  18. -- подсчет числа актеров, которые снимались > 7 фильмах после 2013 (конструкция FROM movie LEFT JOIN actor)
  19. SELECT COUNT(*)
  20. FROM
  21.   (SELECT a1.actor_id
  22.   FROM movie AS m1
  23.   LEFT JOIN film_actor AS fa1 ON m1.film_id=fa1.film_id
  24.   LEFT JOIN actor AS a1 ON fa1.actor_id=a1.actor_id
  25.   WHERE m1.release_year > 2013
  26.   GROUP BY a1.actor_id
  27.   HAVING a1.actor_id > 7) AS query1
  28.  
  29. -- подсчет числа актеров, которые снимались > 7 фильмах после 2013 (конструкция FROM actor LEFT JOIN movie)
  30. SELECT COUNT(*)
  31. FROM
  32.   (SELECT a1.actor_id
  33.   FROM actor AS a1
  34.   LEFT JOIN film_actor AS fa1 ON a1.actor_id=fa1.actor_id
  35.   LEFT JOIN movie AS m1 ON fa1.film_id=m1.film_id
  36.   WHERE m1.release_year > 2013
  37.   GROUP BY a1.actor_id
  38.   HAVING a1.actor_id > 7) AS query2
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement