title: Retrieve top-level parent MySQL
body: <p>I have the following table:</p>
  
  <pre><code>  id  |  parent_id  |  searchable  |  value
  --------------------------------------------
    1   |      0      |      0       |    a
    2   |      1      |      0       |    b
    3   |      2      |      1       |    c
    4   |      0      |      0       |    d
    5   |      4      |      1       |    e
    6   |      0      |      0       |    f
    7   |      6      |      0       |    g
    8   |      6      |      0       |    h
    9   |      0      |      1       |    i
  </code></pre>
  
  <p>I need to extract all the top level records (so the ones where the <code>parent_id = 0</code>).
  But only the records where the parent OR one of his children is searchable (<code>searchable = 1</code>)</p>
  
  <p>So in this case, the output should be:</p>
  
  <pre><code>  id  |  parent_id  |  searchable  |  value
  --------------------------------------------
    1   |     0       |      0       |    a
    4   |     0       |      0       |    d
    9   |     0       |      1       |    i
  </code></pre>
  
  <p>Because these are all top-level records and it self or one of his childeren (doesn't matter how 'deep' the searchable child is) is searchable.</p>
  
  <p>I am working with MySQL. I am not really sure if it is possible to write this with just one query, but I assume it should be done with a piece of recursive code or a function. </p>
  
  <p>** Note: it is unknown how 'deep' the tree goes. </p>