title: Get records where parent id is null and replace data with records which have 'parent id'=id
body: <p>I have table:</p>
  
  <pre><code>+-------+-------------+-----------+-------------+
  | ID    | parent_id   | name      | other_data  | 
  +-------+-------------+-----------+-------------+
  | 1     |  null       | name      | ...         |
  | 2     |  null       | name1     | ...         |
  | 3     |  null       | name2     | ...         |
  | 4     |  2          | name1new  | ...         |
  | 5     |  3          | name2new  | ...         |
  | 6     |  3          | name2new2 | ...         |
  +-------+-------------+-----------+-------------+  
  </code></pre>
  
  <p>And I need all records where parent_id is null. Simple query can be like:</p>
  
  <pre><code>    SELECT id, name
          FROM  `table` 
          WHERE  `parent_id` IS NULL 
          LIMIT 0 , 30
  </code></pre>
  
  <p>It would return 3 records:</p>
  
  <pre><code> 1 name;
   2 name1;
   3 name2
  </code></pre>
  
  <p>What I want is to get these 3 records but replace second two with new values from elements having parent_id as current ID (e.g. 4 row has parent_id 2 so in second row new data must come from row 4. But both: row 5 and row 6 has parent_id 3, so 3 row must have data from row 6 -- the newer one).
  I need result like:</p>
  
  <pre><code>1 name; 
  4 name1new;
  6 name2new2;
  </code></pre>
  
  <hr>
  
  <p>SOLVED:</p>
  
  <pre><code>SELECT
  (CASE WHEN  `t2`.`parent_id` is NOT NULL THEN `t2`.`id` ELSE `t1`.`id` END) as `new_id`,
  (CASE WHEN  `t2`.`parent_id` is NOT NULL THEN `t2`.`name` else `t1`.`name` END) as `new_name`
  FROM `table` as `t1` LEFT JOIN `table` as `t2` on (`t1`.`id` = `t2`.`parent_id` AND `t2`.`some_id` = '6') WHERE `t1`.`parent_id` IS NULL;
  </code></pre>
  
  <p>THX for help @Vishal Zanzrukia</p>