122

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)
Raouf Athar
  • 1,803
  • 2
  • 16
  • 30
  • possible duplicate of [MySQL: View with Subquery in the FROM Clause Limitation](http://stackoverflow.com/questions/206062/mysql-view-with-subquery-in-the-from-clause-limitation) – Matt Fenwick Feb 17 '12 at 17:38
  • @MattFenwick, no it is not - that query can be rewritten easily, which is not possible in general – Tomas Apr 26 '13 at 08:19
  • Subqueries are supported in mariadb `10.2` from version `10.2.1` See - https://jira.mariadb.org/browse/MDEV-3944 – Adarsh Madrecha Feb 09 '18 at 20:38

4 Answers4

166

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

Nonym
  • 6,199
  • 1
  • 25
  • 21
18

As the more recent MySQL documentation on view restrictions says:

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

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.

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.

Shadow
  • 33,525
  • 10
  • 51
  • 64
17
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)
Michał Powaga
  • 22,561
  • 8
  • 51
  • 62
0

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.

mayersdesign
  • 5,062
  • 4
  • 35
  • 47