title: SQL query / procedure to convert hierarchy into nested set
body: <p>I've a lot of classic (mysql) tables with id and parent_id which I need to convert into n <a href="http://en.wikipedia.org/wiki/Nested_set_model" rel="nofollow">nested set</a>. For each row I need to calculate depth (which can vary) and left / right node. </p>
  
  <p>Is it possible to do it with mySQL query or stored procedure? </p>
  
  <p>Edit: depth can vary, so nested joins aren't best solution.</p>