Skip to content
Advertisement

How do I recursively find all matching rows in a table?

I’m trying to wrangle an existing database without resorting to tricks. I need to query within a linking table to find all matching id’s recursively.

I’ve tried a number of nested join queries, but I’m not a SQL expert. I’m a programmer and while I work with simple databases complex queries like this are a struggle for me.

My table looks like this:

I only know the top-level parent id. I want to find all associated child ID’s.

If the parent ID was 7, I need to return 16,19,2,3,4,20,26.

Advertisement

Answer

NOTE: This solution will work in MariaDB 10.2.2 and above.

Try Common Table Expression:

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