Jump to content

Item and First 3 Comments in one query


jimmyoneshot

Recommended Posts

I've been working on this for a while but basically I want to be able to get a list of items (photos) with the first 3 comments on each item displayed beneath each item. At the moment I have the following:-

 


SELECT DISTINCT 

@rowtype:='1' AS rowType,
p.id AS rowId,
p.id AS id,
p.authorId

FROM z_photos AS p

UNION

SELECT

@rowtype:='2' AS rowType,
c.id2 AS rowId,
c.id,
c.authorId

FROM    (
        SELECT  id2,
                COALESCE(
                (
                SELECT  id
                FROM    z_comments li
                WHERE   li.id2 = dlo.id2
                ORDER BY
                        li.id2, li.id
                LIMIT 2, 1
                ), CAST(0xFFFFFFFF AS DECIMAL)) AS mid
        FROM    (
                SELECT  DISTINCT id2
                FROM    z_comments dl
                ) dlo
        ) lo, z_comments c

WHERE  c.id2 >= lo.id2 AND c.id2 <= lo.id2 AND c.id <= lo.mid

ORDER BY rowId DESC, rowType ASC, date DESC

 

But I now want to do this without the union by combining both queries into one. The reason being I want to be able to apply an overall WHERE statement at the end such as "WHERE p.authorId = 7" which will get all photos by that user and the comments on those as the above query will obviously return all comments.

 

Is this possible without the above union or will I have to append such a where statement to the photos query and the comments query?

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.