// MySQL error reference

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
Error message
%s near '%s' at line %d

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, key or rank (reserved since 8.0).
  • A missing or extra comma, parenthesis or quote.
  • Syntax from another database: TOP, [brackets], ISNULL() from SQL Server, or :: casts and RETURNING from PostgreSQL.
  • Syntax newer than the server, such as window functions or CTEs on MySQL 5.7.
  • A script with DELIMITER or stored-program bodies sent through a driver that doesn't understand DELIMITER (it's a mysql client command, not SQL).

How to fix it

  1. Look at the quoted text after near and the token just before it. If the quoted text is empty (near ''), the statement ended too early, often an unclosed quote or parenthesis.

  2. Quote identifiers that are reserved words with backticks.

    SELECT `order`, `rank` FROM `group` WHERE `key` = 1;
  3. Check the server version before using newer syntax.

    SELECT VERSION();
  4. Rewrite other dialects' syntax: LIMIT instead of TOP, 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

More syntax and statements errors

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.

// fewer errors, faster fixes

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.