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
https://forums.phpfreaks.com/topic/255387-item-and-first-3-comments-in-one-query/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.