jimmyoneshot Posted January 19, 2012 Share Posted January 19, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/255387-item-and-first-3-comments-in-one-query/ Share on other sites More sharing options...
fenway Posted January 22, 2012 Share Posted January 22, 2012 Why do you want to combine them? Quote Link to comment https://forums.phpfreaks.com/topic/255387-item-and-first-3-comments-in-one-query/#findComment-1310130 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.