视图的 SELECT 在 FROM 子句中包含子查询

View#39;s SELECT contains a subquery in the FROM clause(视图的 SELECT 在 FROM 子句中包含子查询)
本文介绍了视图的 SELECT 在 FROM 子句中包含子查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

我使用以下查询来执行此操作.没有创建视图"部分的查询运行良好,但使用创建视图"时,它显示错误视图的 SELECT 包含 FROM 子句中的子查询".可能是什么问题&可能的解决方案:

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 子句中包含子查询.

您的解决方法是为每个子查询创建一个视图.

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

然后从您的视图中访问这些视图 view_credit_status

Then access those views from within your view view_credit_status

这篇关于视图的 SELECT 在 FROM 子句中包含子查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)