Database Trees with MySql
I don't think this syntax will work:

IN (`a`)


Instead, you probably need to use CONCAT, etc to construct the SELECT, then PREPARE, EXECUTE, and DEALLOCATE it.

But, I suggest you back up -- Try the SELECTs by hand until you have some confidence that they will deliver the desired results.  Only then try to turn it into a Stored Procedure.