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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8428641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:57:53  来源:igfitidea点击:

View's SELECT contains a subquery in the FROM clause

mysqlsqldatabaseviews

提问by Raouf Athar

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:

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

回答by Nonym

As per documentation:

根据文档:

MySQL Docs

MySQL 文档

  • The SELECT statement cannot contain a subquery in the FROM clause.
  • SELECT 语句不能在 FROM 子句中包含子查询。

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

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

Then access those views from within your view view_credit_status

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

回答by Micha? Powaga

create view view_clients_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(t1.credits_used,0) as used 
    from credit_orders
    left outer join view_clients_credit_usage as t1 on t1.client_id = credit_orders.client_id
    where credit_orders.payment_status='Paid'
    group by credit_orders.client_id)

回答by Shadow

As the more recent MySQL documentation on view restrictionssays:

正如关于视图限制的最新 MySQL 文档所说:

Before MySQL 5.7.7, subqueries cannot be used in the FROM clause of a view.

在 MySQL 5.7.7 之前,不能在视图的 FROM 子句中使用子查询。

This means, that choosing a MySQL v5.7.7 or newer or upgrading the existing MySQL instance to such a version, would remove this restriction on views completely.

这意味着,选择 MySQL v5.7.7 或更新版本或将现有 MySQL 实例升级到这样的版本,将完全取消对视图的这种限制。

However, if you have a current production MySQL version that is earlier than v5.7.7, then the removal of this restriction on views should only be one of the criteria being assessed while making a decision as to upgrade or not. Using the workaround techniques described in the other answers may be a more viable solution - at least on the shorter run.

但是,如果您的当前生产 MySQL 版本早于 v5.7.7,那么取消对视图的这种限制应该只是在决定是否升级时评估的标准之一。使用其他答案中描述的变通技术可能是更可行的解决方案 - 至少在短期内是这样。

回答by user3809638

Looks to me as MySQL 3.6 gives the following error while MySQL 3.7 no longer errors out. I am yet to find anything in the documentation regarding this fix.

在我看来,MySQL 3.6 给出了以下错误,而 MySQL 3.7 不再出错。我还没有在文档中找到有关此修复程序的任何内容。