The Exact Error
ERROR 1064 (42000): 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 'order FROM users WHERE id = 1' at line 1
Quick summary: The parser hit an unexpected token. The word shown in
near '...'is where parsing failed — but the actual bug is usually just before it: a missing comma, unquoted reserved word, or unclosed string.
Why This Error Happens
1. Using a reserved word as a column or table name — order, group, key, index, select, from
2. Missing comma in column list — SELECT id name FROM users (missing , between id and name)
3. Single quotes vs backticks confusion — using single quotes for identifiers instead of backticks
4. Unclosed string literal — WHERE name = 'O'Brien' — apostrophe breaks the string
5. Wrong MySQL version syntax — features available in MySQL 8 but not 5.7
Step-by-Step Diagnosis
Step 1 — Read what's in near '...'
near 'order FROM users' at line 1
The word order is a MySQL reserved word. It needs backticks.
Step 2 — Count your commas
-- WRONG — missing comma after created_at:
SELECT id, name, email created_at FROM users;
-- RIGHT:
SELECT id, name, email, created_at FROM users;
Step 3 — Check for unescaped quotes in strings
-- WRONG — apostrophe ends the string early:
SELECT * FROM users WHERE last_name = 'O'Brien';
-- RIGHT — escape the apostrophe:
SELECT * FROM users WHERE last_name = 'O'Brien';
-- Or use double quotes inside single-quoted string (MySQL allows this):
SELECT * FROM users WHERE last_name = "O'Brien";
Solutions
Solution 1 — Backtick reserved words
-- WRONG:
SELECT order, group, key FROM orders;
-- RIGHT:
SELECT `order`, `group`, `key` FROM orders;
Common reserved words to watch for: order, group, key, index, select, from, where, join, table, column, database, schema, status, type, value
Solution 2 — Fix the comma list
-- Use a formatter or lint: every column after the first needs a comma before it
SELECT
id,
name,
email,
created_at
FROM users;
Solution 3 — Escape special characters in strings
-- Escape apostrophe:
INSERT INTO users (bio) VALUES ('It's a great day');
-- Or use parameterized queries (best practice):
-- Node.js with mysql2:
connection.execute(
'INSERT INTO users (bio) VALUES (?)',
["It's a great day"]
);
Solution 4 — Use parameterized queries to avoid all quoting issues
// mysql2 — never build SQL with string concatenation
const [rows] = await pool.execute(
'SELECT * FROM users WHERE email = ? AND status = ?',
[email, 'active']
);
Real-World Examples
ORM-generated query with reserved word column:
-- If your ORM generates: SELECT order FROM purchases
-- You need to alias or quote the column in your schema definition
-- Sequelize — use field option to map to a reserved word
status: {
type: DataTypes.STRING,
field: '`order`' // Map JS property to backtick-quoted column
}
Quick Reference — MySQL Error 1064 Causes
Pattern in near '...' | Likely cause |
|---|---|
| Reserved word | Missing backticks around identifier |
| Column name | Missing comma before this column |
) or FROM | Unclosed parenthesis in subquery |
Empty string '' | Statement ends unexpectedly |
'string | Unclosed string literal |
Prevent This Error in the Future
1. Use a SQL formatter to catch missing commas and syntax issues before running.
2. Use parameterized queries — never concatenate user input into SQL strings.
3. Avoid naming columns after reserved words — type, order, key, status are common traps.
Use ToolNinja to Debug Faster
The SQL Formatter formats and highlights your SQL, making missing commas, mismatched parentheses, and unquoted reserved words immediately visible.