I have the need to render a threaded view of a 2-levels hierarchical commenting system. The comments are stored in a database. The information about the hierarchy is given by field 'parent_id' (which is 0 for the top-level comments). I cannot change the structure of the database. The present solution is by means of multiple SQL queries:
- an SQL query is performed to fetch all top-level comments
- the code loops through the top-level comments and for each of them performs an SQL query to fetch its children
Now I wonder if a solution with only one SQL query to fetch all the comments as they are followed by the code suggested here to sort them by threads could be more efficient. Any reccomendation?
Thanks, Luciano