MYSQL - 订购查询

时间:2016-11-11 21:30:56

标签: ios mysql swift uitableview

我正在查询我的评论。

但我似乎无法以正确的顺序得到它们。

我想获得最近的第一次,我正在做的事情。但是当我在tableView(ios应用程序)中填充它们时,最新的评论位于顶部。这是正确的。

我需要做的就是像我一样得到最新的评论,但我需要在底部而不是在顶部的第一个(最新的)......

有点像instagram / facebook如何在评论页面上显示评论。

我不确定在下载评论后还是在查询中我是否应重新安排评论。

这就是我查询评论的方式。

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

提前感谢任何有帮助的人。

3 个答案:

答案 0 :(得分:1)

你有没有尝试过:

按日期命令DESC

这会将您的排序更改为DESCending订单而不是ASCending订单

另外(无关)......我活着避免使用"日期"作为列名,并且更喜欢使用在任何编程语言中永远不会成为保留字的单词...例如" comment_date"或" commentDate"

答案 1 :(得分:1)

Not sure if this is what you want:

You start with the tableView at the bottom(latest message) and when you scroll up you will get older messages.

enter image description here

Keep your query in ascending order. So that the first message in the result array is the oldest.

After you load the messages sorted and call tableView.reloadData(), you can scroll to the bottom

var messages: [String] = [String](count: 100, repeatedValue: "")

override func viewDidLoad() {
    super.viewDidLoad()
    for i in 0..<100{ //message 0 is the oldest
        messages[i] = "Message \(i)"
    }
    tableView.reloadData()
}

override func viewWillAppear(animated: Bool) {
    if (self.messages.count > 0){
        self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: self.messages.count-1, inSection: 0), atScrollPosition: .Bottom, animated: false)
    }
}

答案 2 :(得分:0)

首先,您需要按DATE DESC订购它们,然后将其添加到查询SELECT * FROM (的开头,并将其添加到结尾以关闭将反转结果的括号) x ORDER BY DATE现在是最近的,而不是在顶部。

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

希望它有所帮助。