How to Fix MySQL Error 1222 (ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT)
You get mysql error 1222 when combined select queries return different numbers of columns. To fix this error, update every combined query so they return the exact same column count. Database servers enforce strict rules when they merge results from two or more tables. When column counts do not match, the engine stops the query right away. It returns the official symbol ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT and SQLSTATE 21000 to the client app. What causes mysql error 1222 in the database engin
Your coding agents run with your access.
Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.
You get mysql error 1222 when combined select queries return different numbers of columns. To fix this error, update every combined query so they return the exact same column count.
Database servers enforce strict rules when they merge results from two or more tables. When column counts do not match, the engine stops the query right away. It returns the official symbol ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT and SQLSTATE 21000 to the client app.
What causes mysql error 1222 in the database engine
The official message says the used select statements have a different number of columns. This happens most often when developers combine two or more queries with a union operator. The database engine must stack the rows from each query into one single result grid. If the first query returns four columns and the second returns three, the grid breaks.
Wildcard queries are a very common trigger for this specific database failure in production. A developer might join two tables that start with the exact same five columns. Later, someone adds a sixth column to just one of those two tables. The next time the app runs the wildcard union query, the column counts fail to match.
Subqueries that compare one row against multiple columns can also trigger this exact error code. If the left side of a comparison expects one value, the subquery must return one column. Returning two or more columns from that inner query forces the parser to reject the run.
How to find and fix the column mismatch in production
Start by finding the exact query that failed in your app logs or database logs. Break the combined query apart into its individual select statements to check each one. Count the output columns for every single part of the query by hand. Do not rely on wildcard selectors when you check tables in a live database.
Run a table description check on each table if the query uses wildcard selectors. This safe read check shows you the exact column list without locking any live rows. You do not need admin rights or full table privileges to inspect the schema structure. Compare the column lists side by side to spot the extra or missing fields.
DPoP (Demonstration of Proof-of-Possession) + Just-in-Time Access: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Rewrite the broken query so every select statement lists its column names explicitly. Explicit column lists stop future schema changes from breaking your combined queries again. If one table lacks a needed column, add a static placeholder value in that select list. This keeps the column count equal across all parts of the union query.
How to prevent query failures during schema updates
Teams can stop this error before it hits production by setting clear query rules. Ban wildcard selectors inside any union query or multi-table view in your code base. Review all database views whenever your team plans to add or drop a table column. Views that combine tables will fail right away if an underlying table changes its width.
When engineers inspect production schemas to fix broken queries, keep data safety rules active. Use read-only database roles that only allow schema checks and safe select queries. If engineers must run test queries against live user tables, apply data masking rules first. Data masking rewrites matched fields like emails, phone numbers, card numbers, and national IDs in results. This lets your team verify column counts and data types without exposing sensitive user fields.
Add wire guardrails and masking beside MySQL
Database grants and TLS setup decide who can reach MySQL. Even so, an open session can run broad updates or read raw personal data from allowed tables. In practice, hoop.dev sits inline in the wire protocol beside the database as a sidecar for your MySQL. On every production query an agent or engineer sends, hoop.dev decides to pass, mask, block, or escalate it for human review.
With hoop.dev MySQL support, guardrails refuse bad queries before they run or hold risky calls for approval, while data masking rewrites matched sensitive values in result rows in memory. Furthermore, hoop.dev holds SOC 2 Type II and generates evidence for GDPR, HIPAA, and PCI DSS programs. Read the docs and source on GitHub.
Frequently asked questions
Does mysql error 1222 only happen with union queries?
No, it is most common in union queries, but row subqueries cause it too. If a subquery returns two columns when the outer filter expects one, this error occurs. Always check both union blocks and inner subqueries when you debug this failure.
How do I fix a union query when one table has fewer columns?
You can add a null value or a default string as a dummy column. Place this dummy value in the select list of the table with fewer columns. This pads the shorter query so both queries return the exact same number of columns.
Why did my working query start throwing error 1222 after a deployment?
A recent schema change likely added or removed a column on one of your tables. If your query uses a wildcard to select all columns, the table width changed. Replace the wildcard with explicit column names to fix the query and prevent repeat failures.
Related guides
In this post