3

Here is my statement for create view and related error message, using MySQL/MySQL WorkBench. I tried the select -- from -- where works. Any hints are appreciated. :)

CREATE VIEW [Products Above Average Price] AS            
SELECT ProductName,Price
FROM Products
WHERE Price>(SELECT AVG(Price) FROM Products)

Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[Products Above Average Price] AS #how to? SELECT ProductName,Price ' at line 1

3
  • 1
    MySQL uses back-ticks instead of []. Commented Aug 27, 2015 at 6:54
  • Works for me, thanks @jarlh. If you could have a formal answer, I can help to mark as answer to benefit other people met with the same issue as well. :) Commented Aug 27, 2015 at 6:57
  • There's already a helpful question: stackoverflow.com/questions/11321491/… Commented Aug 27, 2015 at 7:09

3 Answers 3

1

You have to use ANSI standard double quotes "" instead of [] brackets.

CREATE VIEW "Products Above Average Price" AS 
SELECT ProductName,Price
FROM Products
WHERE Price>(SELECT AVG(Price) FROM Products)
Sign up to request clarification or add additional context in comments.

Comments

1

Here is how view creation is supposed to look like in MySQL

Create View `MyViewName` as 
    Select
        col1, col2, col3
    From
        myTable T

In your case, the problem occures because of [] which aren't used in MySQL. Replace your query with

CREATE VIEW `Products Above Average Price` AS            
    SELECT ProductName,Price
    FROM Products
    WHERE Price>(SELECT AVG(Price) FROM Products)

Comments

0

(Copied comment) ANSI SQL has double quotes ("") for delimited identifiers, e.g.

CREATE VIEW "Products Above Average Price" AS...

MS SQL Server also has [] as a vendor specific way to delimit identifiers, e.g.

CREATE VIEW [Products Above Average Price] AS...

MySQL has back-ticks, e.g.

CREATE VIEW `Products Above Average Price` AS...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.