Skip to content
Advertisement

WITH queries with INSERT in MySQL

I’m trying to do something that I’m almost positive works in Postgresql:

In other words, I want to grab the “something” row to be used for a bunch of new rows in “somwhere”. Each new entry should have a reference (like foreign key value) back to the same “something” row in the cte, mostly just to avoid a mess. However, MySQL is giving me that “blank stare” syntax error mechanism. Does MySQL allow WITH ... INSERT ...;?

Here’s the actual code; it’s just setting up test data:

Advertisement

Answer

In MySQL the INSERT goes before the CTE definition, i.e.

Example on DB Fiddle

So I think the following would work:

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