gpt4 book ai didi

mysql - View 的 SELECT 包含 FROM 子句中的子查询

转载 作者:IT老高 更新时间:2023-10-28 12:50:49 26 4
gpt4 key购买 nike

我有两个表,我需要创建一个 View 。这些表格是:

credit_orders(id, client_id, number_of_credits, payment_status)
credit_usage(id, client_id, credits_used, date)

我使用以下查询来执行此操作。没有“创建 View ”部分的查询运行良好,但使用“创建 View ”,它显示错误“ View 的 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 Docs

  • SELECT 语句的 FROM 子句中不能包含子查询。

您的解决方法是为每个子查询创建一个 View 。

然后从您的 View 中访问这些 View view_credit_status

关于mysql - View 的 SELECT 包含 FROM 子句中的子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8428641/

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