Incident Response

MySQL ERROR 1054: Unknown column in 'field list'

Sometimes it is a typo. Often it is a quote in the wrong place, an alias MySQL cannot see yet, or a release that shipped code before the migration. Here is how to read the second half of the message, the usual causes, and how to avoid 1054 storms when you change columns on big, busy tables.

· 13 min read · AG Data Team

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.

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

  1. A string without quotes. WHERE city = Riga asks for a column called Riga. String values need single quotes.
  2. Double quotes under ANSI_QUOTES. The identifiers page explains that with ANSI_QUOTES enabled, double quotes quote identifiers, and the string literals page adds that strings may then use only single quotes. The ANSI combination mode includes ANSI_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.
  3. An alias in WHERE. Standard SQL disallows it, as the alias documentation says, because the value may not be computed yet when WHERE runs. Aliases do work in GROUP BY, ORDER BY and HAVING; otherwise repeat the expression or wrap the query in a derived table.
  4. A comma join mixed with JOIN. The JOIN documentation shows the classic case: JOIN has higher precedence than the comma, so t1, t2 JOIN t3 ON ... means t1, (t2 JOIN t3), and the ON clause cannot see t1. Write every join as an explicit JOIN.
  5. The wrong table qualifier. o.email when email lives in customers 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.

-- 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

MySQL Errors SQL Deployments
Work with us

Releases keep breaking on schema changes?

We plan and run MySQL schema changes on large production tables, set up expand and contract releases and check schemas across all your servers.

Get in touch More from the blog