I am getting a MySQL 1349
error, but it appears as though the error is incorrect:
ERROR 1349: View's SELECT contains a subquery in the FROM clause
Can I not have ANY subqueries when creating a view?
Here is my SQL:
CREATE VIEW `wordpress`.`ffi_be_v_book_details` AS (
SELECT ffi_be_courses. * , COALESCE( `Total` , 0 ) AS `Total`
FROM `ffi_be_courses`
LEFT JOIN (
SELECT * , COUNT( `Course` ) AS `Total`
FROM ffi_be_courses
RIGHT JOIN (
SELECT `Course`
FROM `ffi_be_bookcourses`
LEFT JOIN `ffi_be_sale` ON ffi_be_bookcourses.SaleID = ffi_be_sale.SaleID
WHERE DATE_ADD( ffi_be_sale.Upload, INTERVAL(
SELECT `BookExpireMonths`
FROM `ffi_be_settings` ) MONTH ) > CURDATE( ) AND ffi_be_sale.Sold = '0'
GROUP BY ffi_be_bookcourses.SaleID
) AS `q1` ON ffi_be_courses.Code = q1.Course
GROUP BY q1.Course
) AS `q2` ON ffi_be_courses.Code = q2.Code
WHERE ffi_be_courses.Type = 'Arts'
ORDER BY ffi_be_courses.Name ASC
)
Thank you for your time.
The above answers are correct. MySQL 5.5 does not allow subqueries in the FROM clause. It is allowed now, however, in MySQL 5.7.7 and later.
You are missing the fact that views in MySQL do not allow subqueries in the
from
clause. They are allowed in theselect
andwhere
andhaving
clauses, however.The documentation is quite clear:
In your case, you can probably rewrite the
from
clause as a correlated subquery in theselect
clause. You can also use multiple layers of views to do what you want.EDIT:
A SELECT statement in SQL has the following clauses:
SELECT
,FROM
,WHERE
,GROUP BY
,HAVING
, andORDER BY
(according to the standard). In addition, MySQL adds things likeLIMIT
, andINTO OUTFILE
. You can see this in the way that MySQL describes theSELECT
clause in the documentation. You can also see this in the documentation for almost any database.Operations such as
join
are part of theFROM
clause (similarlyWITH ROLLUP
is part of theGROUP BY
andDESC
is part of theORDER BY
). These may seem like arcane syntactic conventions, but it becomes important when there is a restriction like the one above.Perhaps one reason for the confusion is an indentation style that goes like:
Where the join statements line up under the
select
. This is misleading. I would write this as:Only the
select
clauses line up under theselect.
Refer to the documentation --
A view definition is subject to the following restrictions:
The SELECT statement cannot contain a subquery in the FROM clause.
Create a separate view for each of the FROM clause.