MySQL error 1064: You have an error in your SQL syntax
MySQL couldn't parse the statement. The text after near shows where parsing stopped; the mistake is usually right there or just before it.
- Error number
1064- Name
ER_PARSE_ERROR- SQLSTATE
42000- Category
- Syntax and statements
What it means
1064 is the parse error. The reference message is the format %s near '%s' at line %d, where the first part is the generic text You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use, the quoted part is the SQL from the point where the parser gave up, and the line number counts from the start of the statement.
The parser stops at the first token it can't fit, so the real mistake is often the token just before the quoted text: a missing comma, an unclosed quote, or a reserved word used as a name.
Common causes
- A reserved word used as a table or column name without backticks, such as
order,group,keyorrank(reserved since 8.0). - A missing or extra comma, parenthesis or quote.
- Syntax from another database:
TOP,[brackets],ISNULL()from SQL Server, or::casts andRETURNINGfrom PostgreSQL. - Syntax newer than the server, such as window functions or CTEs on MySQL 5.7.
- A script with
DELIMITERor stored-program bodies sent through a driver that doesn't understandDELIMITER(it's a mysql client command, not SQL).
How to fix it
-
Look at the quoted text after
nearand the token just before it. If the quoted text is empty (near ''), the statement ended too early, often an unclosed quote or parenthesis. -
Quote identifiers that are reserved words with backticks.
SELECT `order`, `rank` FROM `group` WHERE `key` = 1;
-
Check the server version before using newer syntax.
SELECT VERSION();
-
Rewrite other dialects' syntax:
LIMITinstead ofTOP, backticks instead of brackets,CAST(x AS ...)instead of::.
Reproduce it safely
SELECT * FROM orders WHERE id = 1 ORDER date; -- ERROR 1064 (42000): You have an error in your SQL syntax; ... near 'date' at line 1
Related errors
1149SQL syntax error1054Unknown column in field list102SQL Server: Incorrect syntax near42601PostgreSQL: syntax error at or nearORA-00933Oracle: SQL command not properly ended
More syntax and statements errors
1052Column is ambiguous1055Expression isn't in GROUP BY1093Can't specify target table for update1136Column count doesn't match value count1242Subquery returns more than 1 row1248Every derived table must have its own alias1318Incorrect number of arguments
The code and message text are from the MySQL 8.4 Error Message Reference. Browse the full MySQL error list or search every database error.
Work on MySQL in SQLly
A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for MySQL and the other databases you use.