A UNION query fails because its SELECT lists do not line up
Your error
ERROR 1222 (21000): The used SELECT statements have a different number of columns1 more line it can showFix it fast
Most common cause, 1 of 3SELECT * reads tables whose columns have drifted apart
Replace the stars with the same named list, in the same order, in every branch.
SELECT id, customer_id, total, created_at FROM orders
UNION ALL
SELECT id, customer_id, total, created_at FROM orders_archive;Short answer
ERROR 1222: The used SELECT statements have a different number of columns means the query blocks of a UNION return unequal column counts: SELECT * over tables that drifted apart, a column added to one branch only, or a missing comma that makes two columns one plus an alias. Name the columns and pad with NULL. MySQL 8.4 and 9.7.
Which cause do you have?
Run every branch of the UNION on its own with LIMIT 1 and the \G terminator. The mysql client then prints one line per column, so the branch with the odd count stands out.
SELECT id, total, created_at FROM orders LIMIT 1\G
SELECT * FROM orders_archive LIMIT 1\GThen find the line you see in this list and open its fix:
- 1SELECT * reads tables whose columns have drifted apart
A branch uses SELECT *, and information_schema reports a different column count for the tables involved - 2One branch lists an extra or a missing expression
The branches name their columns, and the \G output of one of them has one line more or fewer than the others - 3A missing comma turned two columns into one column and an alias
The branches look equal when read quickly, but one of them has two names side by side with no comma between them
What does the error look like?
ERROR 1222 (21000): The used SELECT statements have a different number of columnsError Code: 1222. The used SELECT statements have a different number of columns
Cause 1 of 3
SELECT * reads tables whose columns have drifted apart
You seeA branch uses SELECT *, and information_schema reports a different column count for the tables involved
Check
Error 1222 appears as soon as one query block returns more columns than another. With SELECT * the count is whatever each table holds today, so a column added to the live table and not to its archive copy, or a view that gained a field, breaks a UNION that used to work.
SELECT TABLE_NAME, COUNT(*) AS columns_count
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME IN ('orders', 'orders_archive')
GROUP BY TABLE_NAME;Fix
Replace the stars with the same named list, in the same order, in every branch. The result takes its column names from the first query block, so write the names you want there.
SELECT id, customer_id, total, created_at FROM orders
UNION ALL
SELECT id, customer_id, total, created_at FROM orders_archive;Verify
The query runs, and adding a column to one table later no longer breaks it.
SELECT COUNT(*) FROM (SELECT id, customer_id, total, created_at FROM orders UNION ALL SELECT id, customer_id, total, created_at FROM orders_archive) AS u;Confirmed by dev.mysql.com
Cause 2 of 3
One branch lists an extra or a missing expression
You seeThe branches name their columns, and the \G output of one of them has one line more or fewer than the others
Check
Hand-written branches drift when a computed field, a label such as 'archived' AS source, or a new column is added to one SELECT and forgotten in the next. MySQL compares only the counts here; it does not match columns by name.
SELECT 'live' AS source, id, total FROM orders LIMIT 1\GFix
Give the shorter branch a placeholder at the same position: NULL, or a literal that makes sense for that branch. Keep the same order in every block, since values are combined by position.
SELECT 'live' AS source, id, total, coupon_code FROM orders
UNION ALL
SELECT 'archive', id, total, NULL FROM orders_archive;Verify
Each row of the result carries a value or NULL in the padded column, and the error is gone.
SELECT source, COUNT(*) FROM (SELECT 'live' AS source, id FROM orders UNION ALL SELECT 'archive', id FROM orders_archive) AS u GROUP BY source;Confirmed by dev.mysql.com
Cause 3 of 3
A missing comma turned two columns into one column and an alias
You seeThe branches look equal when read quickly, but one of them has two names side by side with no comma between them
Check
AS is optional for column aliases, and the MySQL manual warns about the side effect: in SELECT columna columnb, MySQL reads columnb as an alias for columna. That branch then returns one column fewer than it appears to, and error 1222 follows.
SELECT id total, created_at FROM orders_archive LIMIT 1\GFix
Put the comma back, and write AS before every real alias so a missing comma cannot hide again.
SELECT id, total AS amount, created_at FROM orders
UNION ALL
SELECT id, total, created_at FROM orders_archive;Verify
The \G output of the fixed branch now lists the same number of columns as the first branch.
SELECT id, total, created_at FROM orders_archive LIMIT 1\GConfirmed by dev.mysql.com
How do I stop it coming back?
- Never use SELECT * inside a UNION in application code or views; name the columns in every branch.
- When a migration adds a column to a table that has an archive or history copy, change the copy in the same migration.
- Write AS before every alias so a forgotten comma shows up in review.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: TaxiDex, QistLock, ShopGo.