Jump to content

Query to get every item and it's top 3 comments


jimmyoneshot

Recommended Posts

I've been trying to do this for weeks and I know it's been done in other ways before but I can't seem to apply it to my code. Basically I want a single query which retrieves all of the items from my posts table and the top 3 comments for each of those posts. This is a simplified version of my tables but basically they are the following:-

 

posts

 

id

text

date

 

comments

 

id

text

date

 

posts_comments (used to create the relationship between posts and comments)

 

id1 (id of the post the comment is on)

id2 (id of the comment)

 

Can anyone please help out with this?

Link to comment
Share on other sites

@fenway - Thanks as always  :) I'll take a look at that now and see if I can get it applied to my example.

 

@The Little Guy - Good question. I need to change this I think. I'm obsessed with using join tables just "incase" I ever need to alter it to many to many at some stage.

 

EDIT - Actually yep sorry about that I posted in the wrong table structure. Ignore that posts_comments table. My postsComments table is actually this:-

 

postsComments

 

id

id2 (id of the post)

text

date

 

Link to comment
Share on other sites

As a novice in sql I'm completely lost to be honest.  :-[ I don't really understand the 'grouper' part of it. I tried the following as a start but can't get it to work:-

 


SELECT  posts.id AS post_id, posts.text AS post_text, posts.date AS post_date, postsComments.id AS comment_id, postsComments.text AS comment_text, postsComments.date AS comment_date
FROM posts postsComments
WHERE postsComments.grouper = @grouper
ORDER BY
postsComments.grouper, postsComments.id
LIMIT 3, 1

Link to comment
Share on other sites

I thought that may have been the case and tried the following:-

 


SELECT  posts.id AS post_id, posts.text AS post_text, posts.date AS post_date, postsComments.id AS comment_id, postsComments.text AS comment_text, postsComments.date AS comment_date
FROM posts postsComments
WHERE postsComments.grouper = posts.id
ORDER BY
postsComments.grouper, postsComments.id
LIMIT 3, 1

 

which resulted in the error - "Unknown column 'posts.id' in 'field list'"

Link to comment
Share on other sites

Your query needs to look something like this:

SELECT  l.*
FROM    (
        SELECT  grouper,
                COALESCE(
                (
                SELECT  id
                FROM    t_limiter li
                WHERE   li.grouper = dlo.grouper
                ORDER BY
                        li.grouper, li.id
                LIMIT 14, 1
                ), CAST(0xFFFFFFFF AS DECIMAL)) AS mid
        FROM    (
                SELECT  DISTINCT grouper
                FROM    t_limiter dl
                ) dlo
        ) lo, t_limiter l
WHERE   l.grouper >= lo.grouper
        AND l.grouper <= lo.grouper
        AND l.id <= lo.mid

where "grouper" is your post ID.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.