首頁 >資料庫 >mysql教程 >如何解決 MySQL 中的「視圖的 SELECT 在 FROM 子句中包含子查詢」錯誤?

如何解決 MySQL 中的「視圖的 SELECT 在 FROM 子句中包含子查詢」錯誤?

Patricia Arquette
Patricia Arquette原創
2025-01-04 05:58:39246瀏覽

How to Resolve

Error: View's SELECT Contains a Subquery in the FROM Clause

問題描述:

問題描述:
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 子句中包含子查詢。

解決方案:

# 创建包含子查询的视图:
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);
您的解決方法是為每個子查詢建立視圖。然後,您可以從視圖 view_credit_status 中存取這些視圖。

以上是如何解決 MySQL 中的「視圖的 SELECT 在 FROM 子句中包含子查詢」錯誤?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn