MySQL ERROR 1349 What I am missing?

2019-02-20 02:39发布

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.

3条回答
Ridiculous、
2楼-- · 2019-02-20 02:47

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.

查看更多
\"骚年 ilove
3楼-- · 2019-02-20 02:59

You are missing the fact that views in MySQL do not allow subqueries in the from clause. They are allowed in the select and where and having clauses, however.

The documentation is quite clear:

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

In your case, you can probably rewrite the from clause as a correlated subquery in the select 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, and ORDER BY (according to the standard). In addition, MySQL adds things like LIMIT, and INTO OUTFILE. You can see this in the way that MySQL describes the SELECT clause in the documentation. You can also see this in the documentation for almost any database.

Operations such as join are part of the FROM clause (similarly WITH ROLLUP is part of the GROUP BY and DESC is part of the ORDER 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:

select . . .
from t1
inner join t2
     on . . .

Where the join statements line up under the select. This is misleading. I would write this as:

select 
from t1 join
     t2
     on . . .

Only the select clauses line up under the select.

查看更多
该账号已被封号
4楼-- · 2019-02-20 03:03

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.

查看更多
登录 后发表回答