View的SELECT包含FROM子句中的子查询(View's SELECT contains a subquery in the FROM clause)

编程入门 行业动态 更新时间:2024-10-21 11:32:36
View的SELECT包含FROM子句中的子查询(View's SELECT contains a subquery in the FROM clause)

我有两个表,我需要创建一个视图。 表格是:

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

我使用以下查询来做到这一点。 没有“创建视图”部分的查询工作良好,但使用“创建视图”,它显示错误“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)

I have two tables and I need to create a view. The tables are:

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

I use the following query to do this. The query without the "create view" part works well but with "create view", it shows the error "View's SELECT contains a subquery in the FROM clause". What could be the issue & possible solution:

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视图中访问这些视图

As per documentation:

MySQL Docs

The SELECT statement cannot contain a subquery in the FROM clause.

Your workaround would be to create a view for each of your subqueries.

Then access those views from within your view view_credit_status

更多推荐

本文发布于:2023-08-05 07:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1428880.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:句中   SELECT   View   subquery   clause

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!