MySQL ERROR 1054: Unknown column in 'field list' means the statement names a column that MySQL cannot find in the tables it sees at that point of the query. The column may not exist, may exist under a different name, may live in another table or schema, or may simply not be visible yet in that clause.
mysql> INSERT INTO customers (name, emial) VALUES ('Anna', 'anna@example.com');
ERROR 1054 (42S22): Unknown column 'emial' in 'field list'
mysql> SELECT id, COUNT(*) AS cnt FROM orders WHERE cnt > 1 GROUP BY id;
ERROR 1054 (42S22): Unknown column 'cnt' in 'where clause'
The server error reference lists it as ER_BAD_FIELD_ERROR, SQLSTATE 42S22, message Unknown column '%s' in '%s'. The second placeholder is the most useful part: it tells you where MySQL was looking.
- 'field list': the select list, or the column list of an
INSERTorUPDATE. - 'where clause': the
WHEREcondition, often an alias or an unquoted string. - 'on clause': a join condition that refers to a table outside the join's operands.
- 'order clause' and others: the same check in other parts of the query.
First check: does the column really exist?
Before you read the query five more times, ask the server. Typos, a column renamed in the last migration, or a connection pointed at the wrong database (staging instead of production, a stale copy) explain a large share of these errors. If the table itself is missing, you would get ERROR 1146 instead, which is a good hint that the schema is right and only the column is off.
-- does the column exist, and with exactly this name?
SHOW COLUMNS FROM customers LIKE 'email%';
SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME
FROM information_schema.COLUMNS
WHERE COLUMN_NAME LIKE '%email%'
ORDER BY TABLE_SCHEMA, TABLE_NAME;
-- are you even in the database you think you are?
SELECT DATABASE();
The query-side causes
- A string without quotes.
WHERE city = Rigaasks for a column calledRiga. String values need single quotes. - Double quotes under ANSI_QUOTES. The identifiers page explains that with
ANSI_QUOTESenabled, double quotes quote identifiers, and the string literals page adds that strings may then use only single quotes. TheANSIcombination mode includesANSI_QUOTES(SQL modes). A query written for the default mode, where"Riga"is a string, breaks on a server or session that runs in ANSI mode. The safe habit: single quotes for strings, backticks for identifiers. - An alias in WHERE. Standard SQL disallows it, as the alias documentation says, because the value may not be computed yet when
WHEREruns. Aliases do work inGROUP BY,ORDER BYandHAVING; otherwise repeat the expression or wrap the query in a derived table. - A comma join mixed with JOIN. The JOIN documentation shows the classic case:
JOINhas higher precedence than the comma, sot1, t2 JOIN t3 ON ...meanst1, (t2 JOIN t3), and theONclause cannot seet1. Write every join as an explicitJOIN. - The wrong table qualifier.
o.emailwhenemaillives incustomers c, or a column that exists in a subquery's table but not in the outer query.
-- 1. a string without quotes is read as a column name
SELECT * FROM customers WHERE city = Riga; -- Unknown column 'Riga'
SELECT * FROM customers WHERE city = 'Riga'; -- correct
-- 2. double quotes with ANSI_QUOTES: "Riga" is an identifier, not a string
SET SESSION sql_mode = 'ANSI';
SELECT * FROM customers WHERE city = "Riga"; -- Unknown column 'Riga'
-- 3. an alias in WHERE: use HAVING, or repeat the expression
SELECT id, COUNT(*) AS cnt FROM orders GROUP BY id HAVING cnt > 1;
-- 4. comma join mixed with JOIN ... ON
SELECT * FROM t1, t2 JOIN t3 ON (t1.i1 = t3.i3); -- Unknown column 't1.i1' in 'on clause'
SELECT * FROM t1 JOIN t2 JOIN t3 ON (t1.i1 = t3.i3);
If the error starts right after a deploy, suspect the release, not the SQL. New code that uses a column the migration has not created yet, or old application servers still running a query against a column that was just dropped, both produce 1054 under load. That is a release ordering problem, and it is the main topic of the next section.
Schema drift between servers
The same query works on the primary and fails on a replica, or works in staging and fails in production. Then the schemas differ, usually because a manual change was made on one server only or a migration failed half way. Do not guess; compare the column lists of every table on every server, using information_schema.COLUMNS:
-- run on every server (primary, replicas, staging) and compare the output
SET SESSION group_concat_max_len = 1000000; -- the default 1024 bytes truncates wide tables
SELECT TABLE_NAME,
COUNT(*) AS columns_count,
MD5(GROUP_CONCAT(COLUMN_NAME, ':', COLUMN_TYPE ORDER BY ORDINAL_POSITION)) AS signature
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'app'
GROUP BY TABLE_NAME
ORDER BY TABLE_NAME;
The group_concat_max_len default is 1024 bytes, so raise it for the session first; otherwise the signature of a wide table is computed from a truncated list and can hide a difference.
What changes on large, busy tables
On a small database you add the column, deploy and move on. On tables with hundreds of millions of rows and thousands of queries per second, the order and the locking decide whether a column change is invisible or causes an outage.
- Expand, then contract. Add new columns before any code uses them, and drop old ones only after every application server, worker and cron job has stopped using them. During a rolling deploy old and new code run side by side, so a column dropped too early produces a wave of 1054 errors from the servers not yet updated. Changing MySQL tables without downtime covers the tools for the steps that cannot be instant.
- INSTANT is fast, but not lock-free. In MySQL 8.4
INSTANTis the default algorithm for adding a column, at any position (online DDL operations). It still needs a brief exclusive metadata lock. The online DDL performance page warns that the operation waits for transactions holding metadata locks on the table, and that a pending exclusive lock blocks subsequent transactions on the table. One long transaction can therefore freeze all traffic to a hot table. Set a shortlock_wait_timeoutfor the DDL session (the default is a year) and retry, rather than letting queries pile up. - Watch the instant row version limit. Each instant add or drop increments
TOTAL_ROW_VERSIONS. The limit is 64 (255 from MySQL 9.1.0); after that instant changes fail with ERROR 4092 and you need a full table rebuild, which on a large table is a planned operation, not a quick fix. Check how close your busiest tables are before a release that adds columns. - Views and stored code are not updated for you. When a column is dropped or renamed, views, procedures and triggers that use it are not rewritten. In our experience they fail only when they next run: a view with error 1356, stored code with 1054. CHECK TABLE can check views for problems such as missing referenced tables; run it on views after a schema change and grep your routines for the old column name.
- Check every server, not just the primary. On a fleet of replicas and shards, run the schema signature query after each migration. In our experience drift is far cheaper to catch then than during the next failover.
-- add the column first, with a short metadata lock wait
SET SESSION lock_wait_timeout = 5;
ALTER TABLE orders ADD COLUMN delivery_note VARCHAR(255) NULL, ALGORITHM=INSTANT;
-- tables close to the instant row version limit (64 in 8.4)
SELECT NAME, TOTAL_ROW_VERSIONS
FROM information_schema.INNODB_TABLES
WHERE TOTAL_ROW_VERSIONS > 50;
-- views broken by a dropped or renamed column
CHECK TABLE app.v_customer_orders;
Key takeaways
- ERROR 1054 (42S22) means MySQL cannot see the column in that part of the query; the clause in quotes tells you where.
- Check the column and the current database first, then look for unquoted strings, double quotes under
ANSI_QUOTES, aliases inWHEREand comma joins. - If it works on one server and not another, compare schemas with
information_schema.COLUMNSinstead of guessing. - On large tables release with expand and contract, use a short
lock_wait_timeoutfor DDL and watchTOTAL_ROW_VERSIONS. - After dropping or renaming columns, check views and stored code; they are not updated automatically.