gpt4 book ai didi

mysql - mysql 中的嵌套 View 问题

转载 作者:行者123 更新时间:2023-11-29 19:56:01 25 4
gpt4 key购买 nike

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;

上面的查询给出了错误:

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

我们如何在一个查询中为上述查询编写嵌套 View ?请帮忙。提前致谢。

最佳答案

您不能在创建 View 中使用子查询,但可以通过这种方式使用其他 View

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
;

关于mysql - mysql 中的嵌套 View 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40696816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com