MySQL Error Code: 1349. View 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;

This Post Has One Comment

  1. No Fault

    MySQL does not support subqueries in views:

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

    The documentation is here.
    CREATE VIEW v1 AS
    SELECT * FROM t2 WHERE EXISTS (SELECT 1 FROM t1 WHERE t1.a = t2.a);

    UPDATE t1, v2 SET t1.a = 1 WHERE t1.b = v2.b;

    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.

Leave a Reply