Procedure for tree view
3 column unique_id, parent_id &amp; node_name, but this query (Given in below) is return only two rows.


MySQL doesn't have Tail Call Optimization. Without TCO, each call will take more memory, and get a different stack. So implementing it in this fashion is at the very best insecure because you can't ensure through a constraint that the query won't result in SQL dying. Case and point, retrying the query in such a case will result in another dead backend. So now you gotta do something other than retry. Any answer that uses this method is pretty misguided. 

So what do you do? Upgrade to MySQL 8 beta. That has Recursive CTEs. This is called a hierarchical query, or even better -- migrate to PostgreSQL which is a superior database that has had this feature for like a decade.

For more information, see the hierarchy tag.