Brian Smith
Brian Smith

Reputation: 1481

Comment reply query (in reverse order)

I found a comments query example on this site that works with perfectly with my current DB structure.

Link : How to make comment reply query in MYSQL?

The accepted answer works, but I was wondering if it was possible to reverse the order so the newest comments show up first?

SELECT *
FROM comments
ORDER BY IF(ParentId = 0, Id, ParentId), Id

I tried "desc" in the query, but that messes up parent / child comment sorting.

Upvotes: 2

Views: 791

Answers (1)

fthiella
fthiella

Reputation: 49049

I think you are looking for this:

SELECT *
FROM comments
ORDER BY IF(ParentId = 0, Id, ParentId), ParentId!=0, Id desc

This will sort all parents in ASC order, then all comments in DESC order, leaving each parent at the top. Please see fiddle here.

Upvotes: 2

Related Questions