Error: View's SELECT Contains a Subquery in the FROM Clause
问题描述:
您正在尝试创建一个视图,但遇到错误“视图的 SELECT 语句包含 FROM 子句中的子查询”。您提供的查询如下:
create view view_credit_status as (select credit_orders.client_id, sum(credit_orders.number_of_credits) as purchased, ifnull(t1.credits_used,0) as used from credit_orders left outer join (select * from (select credit_usage.client_id, sum(credits_used) as credits_used from credit_usage group by credit_usage.client_id) as t0 ) as t1 on t1.client_id = credit_orders.client_id where credit_orders.payment_status='Paid' group by credit_orders.client_id)
原因:
根据 MySQL 文档,SELECT 语句不能在 FROM 子句中包含子查询。
解决方案:
您的解决方法是为每个子查询创建视图。然后,您可以从视图 view_credit_status 中访问这些视图。
# 创建包含子查询的视图: create view sum_credit_usage as select client_id, sum(credits_used) as credits_used from credit_usage group by client_id; # 创建您的最终视图: create view view_credit_status as (select credit_orders.client_id, sum(credit_orders.number_of_credits) as purchased, ifnull(sum_credit_usage.credits_used, 0) as used from credit_orders left outer join sum_credit_usage on t1.client_id = credit_orders.client_id where credit_orders.payment_status='Paid' group by credit_orders.client_id);
以上是如何解决 MySQL 中的'视图的 SELECT 在 FROM 子句中包含子查询”错误?的详细内容。更多信息请关注PHP中文网其他相关文章!