0
CREATE or REPLACE VIEW myView AS
SELECT t2.film_id, t2.no_actors AS n_actor, COALESCE(t1.no_copies,0) AS n_inventory FROM
(SELECT film_id, count(film_id) AS no_copies FROM inventory GROUP BY film_id) AS t1 RIGHT OUTER JOIN 
(SELECT film_id, count(actor_id) 
AS no_actors FROM film_actor GROUP BY film_id) AS t2 
ON t2.film_id=t1.film_id;

The above query gives the error:

ERROR 1349 (HY000): View's SELECT contains a subquery in the FROM clause

How do we write nested views for the above query in one single query ? Please help. Thanks in advance.

ScaisEdge
  • 131,976
  • 10
  • 91
  • 107

1 Answers1

1

You can't use subquery in create view but you can use others views this way

CREATE or REPLACE VIEW myViewSub1 AS 
SELECT film_id, count(film_id) AS no_copies FROM inventory GROUP BY film_id
;


CREATE or REPLACE VIEW myViewSub2 AS 
SELECT film_id, count(actor_id) 
AS no_actors FROM film_actor GROUP BY film_id
;

CREATE or REPLACE VIEW myView AS
SELECT t2.film_id, t2.no_actors AS n_actor, COALESCE(t1.no_copies,0) AS n_inventory 
FROM  myViewSub1 AS t1 
RIGHT OUTER JOIN myViewSub2 AS t2 
ON t2.film_id=t1.film_id
;
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107