Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

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)

1 Answer

0 votes
by (40.7k points)

The SELECT statement doesn't contain a subquery in the FROM clause.

Note: If you want to create a view for each of your subqueries then, try to access those views from within your view view_credit_status

For more information, you can refer to MySQL Docs

Related questions

0 votes
2 answers
0 votes
1 answer
0 votes
1 answer
asked Dec 18, 2020 in SQL by Appu (6.1k points)
0 votes
1 answer

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...