Skip to content
Advertisement

How To Write A Query With A CTE And A Left Join

I am trying to have a calendar table with my CTE and set it up so that my dates in my query display like this

Jan 18
Jan 19
Feb 18
Feb 19

Now this is my DDL and this is the query I attempted, but in MySql Workbench I’m getting the error that there is an error in my sql somewhere.

This is the exact error:

Query Error: Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘RECURSIVE cte_months_to_pull AS ( SELECT DATE_FORMAT(@start_date, ‘%Y-%m-01” at line 1

Can someone assist?

If it’s easier this is a SQL Fiddle of everything http://sqlfiddle.com/#!9/300f9d/1

And this is the query I try:

Advertisement

Answer

As I can see you are using MySQL version older than 8.0 which doesn’t support RECURSIVE CTEs. I have tried your query with some minor updates on 8.0 and it worked fine –

Here is the Fiddle

Since there is no expected output, I have only tried till running the query.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement