Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Error Code: 1349. View's SELECT contains a subquery in the FROM clause

I am trying to create a view but get the following error:

View's SELECT contains a subquery in the FROM clause

I am running the following command. I can't seem to figure out how to substitute the nested selects with joins. Any help would be much appreciated!

create view student_fee_basic as
select fsbc.*, ffp.name, ffp.amount 'fee'
from
(select sbc.*, ffc.name 'fname', ffc.id 'fid'
    from (select s.admission_no, s.first_name, bc.id 'bid', bc.code, bc.name
        from (select b.id, b.name, c.code
            from batches b, courses c
            where b.name = '2014-2015'
            and b.course_id = c.id) bc
        left join students s on bc.id = s.batch_id) sbc
    left join finance_fee_categories ffc on ffc.batch_id = sbc.bid
    where ffc.name = 'Basic Monthly') fsbc
left join finance_fee_particulars ffp on ffp.finance_fee_category_id = fsbc.fid;
like image 320
haanimasood Avatar asked May 20 '14 16:05

haanimasood


2 Answers

MySQL does not support subqueries in views:

Subqueries cannot be used in the FROM clause of a view.

The documentation is here.

The easiest fix is to use a series of different views for each level.

You can probably rewrite this query to remove the subqueries. However, I find it very hard to help without explicit joins.

like image 120
Gordon Linoff Avatar answered Oct 09 '22 06:10

Gordon Linoff


Version 5.7 supports it. So one way to fix it is to migrate your database to newer version

like image 30
walv Avatar answered Oct 09 '22 06:10

walv