3289
MySQLERRORNotableCTEsHIGH confidence

Recursive CTE self-reference must be leftmost in JOIN

Production Risk

Low — query is rejected; reorder JOIN tables.

How to reproduce
trigger — this will error
trigger — this will error
WITH RECURSIVE cte AS (SELECT 1 AS n UNION ALL SELECT n+1 FROM other_table JOIN cte WHERE n < 5) SELECT * FROM cte;

expected output

ERROR 3289 (HY000): Recursive CTE 'cte' self-reference must be leftmost table.

Fix

Put CTE reference leftmost

Put CTE reference leftmost
WITH RECURSIVE cte AS (SELECT 1 AS n UNION ALL SELECT n+1 FROM cte JOIN other_table WHERE n < 5) SELECT * FROM cte;

Why this works

The recursive self-reference must be the leading table in the FROM/JOIN clause.

What not to do

Sources
Official documentation ↗

MySQL 8.0 — 3289 ER_CTE_RECURSIVE_FORBIDDEN_JOIN_ORDER

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All MySQL errors