Jump to content

Recommended Posts

My situation is strange but basically I need to return a list of items and their comments in 1 query. The problem is this, I need to order the items from oldest to newest (bottom as oldest to top as newest) with all of the comments for that item displayed beneath each item with the oldest comment on top and the newest comment on the bottom. Very similar to Facebook. Like this:-

 

item 3

  comment1

  comment2

  comment3

 

item2

  comment1

  comment2

 

item1

  comment1

  comment2

  comment3

 

I've managed to get this data retrieved 1 query but it is the order by/group by methods which I need to employ that I am struggling to figure out. Here is what I've got so far:-

 


SELECT

@item:='item' AS row_type,
items.id AS row_item_id,
items.id AS id
items.body AS body
items.date AS date

UNION ALL

SELECT

@comment:='comment' AS row_type,
items.id AS row_item_id,
item_comments.id AS id
item_comments.body AS body
item_comments.date AS date

LEFT JOIN items
ON (item_comments.id2 = items.id)

GROUP BY row_item_id ORDER BY id desc

 

The above query is a very simplified version of my full query but basically this groups everything correctly I THINK but it is getting the desired ordering of items with newest on top and comments with newest on bottom which I can't figure out. Can anybody please help? Thanks in advance

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.