Jack Jack - 19 days ago 7
iOS Question

MYSQL - Ordering query

I am querying comments from my table.

But I cannot seem to get them in the correct order.

I would like to get the most recent first, which I am managing to do. But when I populate them in my tableView(ios app) the lastest comment is at the top. Which is correct.

What I need to do is get the latest comment like I am doing but I need the first(latest) at the bottom instead of at the top..

A bit like how instagram/facebook display there comments on the comment page.

I am not sure if I should re-arrange the comments once I have downloaded them or in the query it self.

This is how I am querying the comments.

SELECT * FROM
(SELECT

C.uuidPost,
C.comment,
C.type,
C.date,
C.uuid,
USERS.id,
USERS.username,
USERS.profileImage
FROM Activity C JOIN USERS ON
C.id = USERS.id
WHERE C.type = 'comment'
AND C.uuidPost = $uuidPost
ORDER BY DATE DESC
LIMIT 0, 15 ) x ORDER BY DATE


Thanks in advance to anyone that can help.

Answer

First you need to order them by DATE DESC and then add this to the begining of the query SELECT * FROM ( and this to the end to close the brackets ) x ORDER BY DATE that will invert the result and present the most recent at the bottom now instead of at the top.

SELECT * FROM 
    (SELECT 

    C.uuidPost,
    C.comment,
    C.type,
    C.date,
    C.uuid,
    USERS.id,
    USERS.username,
    USERS.profileImage
    FROM Activity C JOIN USERS ON
    C.id = USERS.id 
    WHERE C.type = 'comment'
    AND C.uuidPost = $uuidPost
    ORDER BY DATE DESC
    LIMIT 0, 15 ) x ORDER BY DATE

Hope it helps.

Comments