My application is extremely slow when I move the logic for this to my scripting language. I'm hoping there's a way to leverage MySQL itself for this.
Consider the following table
ID' col1 ' ' col2'
-----------------
1 '   1  ' '  2  '
-----------------
2 '   2  ' '  3  '
-----------------
3 '   3  ' '  4  '
-----------------
4 '   5  ' '  4  '
-----------------
Is there a way to select all records where col1 is 1 or whether col1 is the col2 of a row from a previous match?
For example, if I say I need all records related to 1, I'm looking to get rows 1, 2 and 3.
Here's a query example but it only goes to the first level of depth.
SELECT col2 FROM table WHERE (col1 = 1) OR (col1 IN (SELECT col2 FROM table WHERE (col1 = 1))) 
 
     
     
    