How to Fix MySQL Error 3573 (ER_CTE_RECURSIVE_REQUIRES_UNION)

Published
Length
3 min read

Luis Felipe Dias

All posts

You trigger mysql error 3573 when a recursive common table expression lacks a union operator. You fix this error by joining your base query and recursive query with a union. MySQL uses the symbol ER_CTE_RECURSIVE_REQUIRES_UNION and SQLSTATE HY000 for this failure. The database engine stops the query before it reads any table rows. It returns a message stating that the recursive expression should contain a union. What causes mysql error 3573 in the database engine A recursive common table exp

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 trigger mysql error 3573 when a recursive common table expression lacks a union operator. You fix this error by joining your base query and recursive query with a union.

MySQL uses the symbol ER_CTE_RECURSIVE_REQUIRES_UNION and SQLSTATE HY000 for this failure. The database engine stops the query before it reads any table rows. It returns a message stating that the recursive expression should contain a union.

What causes mysql error 3573 in the database engine

A recursive common table expression needs two distinct parts to work in MySQL. The first part is the seed query, which sets the starting rows. The second part is the recursive query, which references the expression name itself.

MySQL requires you to join these two parts with a union operator. If you mark an expression as recursive without a union, the parser rejects it. This rule prevents infinite loops that lack a clear starting point.

Developers often cause mysql error 3573 in two ways. Sometimes they add the recursive keyword to a standard query by mistake. Other times they write the seed query and forget to append the recursive step.

Query builders in an app can also trigger this bug. If a code branch skips the union clause, the generated statement fails right away. The parser catches this syntax mistake before execution begins.

How to diagnose and fix the failure in production

Start by finding the exact query that caused the error in your logs. Look at the error message to see the name of the broken expression. The engine inserts the expression name into the error text.

Check whether the query actually needs to walk a tree or hierarchy. If the query does not reference itself, remove the recursive keyword from the statement. The query will then run as a standard common table expression.

MySQL Access Governance + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

If the query must be recursive, check the structure inside its brackets. Make sure the seed block comes first and does not reference the expression. Then place a union or union all operator right after the seed block.

Next, write the recursive block after the union operator. This second block must reference the expression name to build the next set of rows. Also include a clear stop condition in the where clause to halt recursion.

How to test recursive queries safely

When you fix queries in production, do not grant broad database roles to developers. Engineers only need read permissions on the relevant tables to test a select query. Keep write permissions locked down during the debug process.

Bad recursive queries can consume lots of memory and CPU time. Always test the fixed query on a small subset of data first. Check that the stop condition works before running it against full tables.

If your tables contain sensitive user details, apply data masking during debug sessions. Masking rewrites matched fields like emails, phone numbers, card numbers, or national IDs in results. This lets your team fix the query logic while keeping private values safe.

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

What does the ER_CTE_RECURSIVE_REQUIRES_UNION symbol mean?

It is the official MySQL symbol for error code 3573 under SQLSTATE HY000. It means you declared a recursive common table expression without a union operator.

Should I use union or union all to fix mysql error 3573?

Both operators fix the error and satisfy the MySQL parser. Use union to remove duplicate rows, or union all to keep duplicates and run faster.

Can I fix this error without changing the query text?

No, because this is a syntax rule enforced by the MySQL query parser. You must either add a union block or remove the recursive keyword in your app.

The query runs. The emails stay hidden.