title: In MYSQL, is it possible to SELECT something that will be defined in the WHERE clause?
body: <p>I have a system which contains items that may each belong to more than one parent. I need to be able to retrieve all of an item's children, and it's children's children.. ad nauseam. OK well there are 5 levels.</p>
  
  <p>Each item has (or should have) a json column named "belongs_to_json" (also more columns such as "id" and whatnot) which contains each parent, order combination. e.g. [{"parent": 2, "order": 4},{"parent": 13, "order": 1}]</p>
  
  <p>I can search for this and others with the same parent by combining PHP and MYSQL (idea found on stackoverflow previously)</p>
  
  <p>First I run a query (codeigniter framework) getting some item parents and put the query in a $qry_classes variable.</p>
  
  <pre><code>    $sql_classes = "SELECT t.id 
      FROM table t 
      WHERE JSON_SEARCH(t.belongs_to_json, 'all', '2') LIKE '%.parent%';";
      $qry_classes = $this-&gt;db-&gt;query($sql_classes);
  </code></pre>
  
  <p>Then I use the results from that query to write a new WHERE statement.</p>
  
  <pre><code>    $s_rewrite_array = array_map(function($row) {
      return "JSON_SEARCH(t.belongs_to_json, 'all', '{$row-&gt;id}') LIKE '%.parent%'";
      }, $qry_classes-&gt;return_array() );
      $sections_condition = implode(" OR ", $s_rewrite_array);
      $sql_sections = "
          SELECT  t.id
          FROM    table t
          WHERE   ($sections_condition)
      ";
      $qry_sections = $this-&gt;db-&gt;query($sql_sections);
  </code></pre>
  
  <p>The problem comes when I have an item with multiple parents and I want to return the parent that was used in the WHERE clause in the SELECT clause.</p>
  
  <p>Something like</p>
  
  <pre><code>    SELECT t.*, non_existant_column FROM table t WHERE 
      JSON_SEARCH(t.belongs_to_json, 'all', '{$row-&gt;id}' as non_existant_column) LIKE '%.parent%;
  </code></pre>
  
  <p>obviously not a real thing but I'm hoping there is an equivalent. Or maybe different way to solve the issue.</p>