Hi I m using MySql with Doctrine to order all the comments a specified user has (either ones created by the user or a reply to a comment posted by the user)
But the problem is there is no order to that query. The result (ideally) should be like this:
id text reply_id created_at
1 a null time()
2 b 1 time()+1
3 c 1 time()+2
4 d null time()-3
I’ve experimented with a few different order by and group by functions but I can’t get it to work. One major problem is that if the comment does not have a reply_id it needs to be ordered by the date DESCENDING. But if there is a reply_id then the date needs to be ASCENDING.
I’m not sure if this is possible using just queries. But any help would be greatly appreciated.
ALright here’s a few sample comments in YAML format.
Comment:
UserComment:
text: |
Hey this is a test comment from cameron.j.moore@gmail.com...isn't this cool.
User: CameronMoore
CommentType: Basic
UserCommentReply:
text: |
This is a reply to the first comment
User: CameronMoore
Comment: UserComment
CommentType: Basic
UserCommentSecondReply:
text: |
This is another reply to the first comment
User: CMoore
Comment: UserComment
CommentType: Basic
AnotherComment:
text: |
Second Comment here.
User: CameronMoore
CommentType: Basic
SecondUserComment:
text: |
Hey another test
User: CMoore
CommentType: Basic
When I query for comments that are by or reference User CameronMoore I would like the result to be ordered like this:
sorry, i don’t understand yaml, but i can sort of figure it out, and it appears you have left off the timestamps, so of course the ORDER BY is difficult to visualize
SELECT main.updated_at AS sortkey1
, NULL AS sortkey2
, main.text
, main.user_id
FROM comment AS main
WHERE main.comment_reply_id IS NULL
UNION ALL
SELECT main.updated_at AS sortkey1
, reply.updated_at AS sortkey2
, reply.text
, reply.user_id
FROM comment AS main
INNER
JOIN comment AS reply
ON reply.comment_reply_id = main.id
WHERE reply.comment_reply_id IS NOT NULL
ORDER
BY sortkey1 DESC
, sortkey2 ASC
unable to confirm that this actually works as expected because all your sample data had the same timestamps!