Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are three (four) alternative possibilities:</p> <ol> <li><p>A <strong>recursive query</strong> to select all comments based on their parent ids. This is supported by many database products and the syntax depends on the database type. Check the docs for more info (search for 'recursive').</p></li> <li><p>If you store the <strong>article id</strong> in each (sub-)comment, you can just select all comments with the article id in one regular select query. You can use the parent ids to properly display the comments on the page under the right parent comment:</p> <pre><code>SELECT * FROM comments WHERE article_id = :article_id </code></pre></li> <li><p>If you only need two levels of comments, you can use an extended <strong>where</strong> to include both first level and second level comments:</p> <pre><code>SELECT * FROM comments WHERE parent_id = :article_id OR parent_id IN (SELECT id FROM comments WHERE parent_id = :article_id) </code></pre></li> <li><p>It is also possible to use <strong>union all</strong> to combine two queries that have the same columns, but since I assume that all data are from the same table, there is probably no need for it (see the extended where-clause above):</p> <pre><code>SELECT * FROM comments WHERE parent_id = :article_id UNION ALL SELECT * FROM comments WHERE parent_id IN (SELECT id FROM comments WHERE parent_id = :article_id) </code></pre></li> </ol> <p>Personally, I would go for option 2 because it is simple (no exotic SQL construct required), efficient (1 query) and flexible (supports as many levels of comments as you like).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload