Home >Database >Mysql Tutorial >How to Resolve 'View's SELECT Contains a Subquery in the FROM Clause' Error in MySQL?
Error: View's SELECT Contains a Subquery in the FROM Clause
Problem description:
You are trying to create a view but encounter the error "View's SELECT statement contains FROM subquery in clause". The query you provided is as follows:
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)
Cause:
According to the MySQL documentation, a SELECT statement cannot contain a subquery in the FROM clause.
Solution:
Your solution is to create a view for each subquery. You can then access these views from the view 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);
The above is the detailed content of How to Resolve 'View's SELECT Contains a Subquery in the FROM Clause' Error in MySQL?. For more information, please follow other related articles on the PHP Chinese website!