0

我想按他们的时间戳对我的帖子进行排序,但事实并非如此,我也不知道为什么。当我在没有顺序的情况下执行查询时,它可以工作,但是当我添加 ORDER BY 时,它不起作用。

这是我的查询:

SELECT * FROM Posts INNER JOIN Topics ON idTopic = idTopics  
INNER JOIN Users ON idUserPosts = idUsers
WHERE idTopic = :idTopic LIMIT :limit_down, :limit_up 
ORDER BY Posts.datePosts DESC;

为什么这不起作用?

4

3 回答 3

2

尝试反转 ORDER BY/LIMIT:

SELECT * FROM Posts 
INNER JOIN Topics ON idTopic = idTopics  
INNER JOIN Users ON idUserPosts = idUsers
WHERE idTopic = :idTopic 
ORDER BY Posts.datePosts DESC
LIMIT :limit_down, :limit_up;

order by 需要在限制之前,否则限制不太有用:您将获得表的前 X 行,但“第一”使用什么标准?然后你会订购这些结果。

如果您真的愿意,您也可以这样做,但请考虑一下您当时在做什么:

SELECT * FROM (
    SELECT Posts.datePosts orderField, * FROM Posts 
    INNER JOIN Topics ON idTopic = idTopics  
    INNER JOIN Users ON idUserPosts = idUsers
    WHERE idTopic = :idTopic 
    LIMIT :limit_down, :limit_up;
)
ORDER BY orderField DESC
于 2012-04-27T12:56:10.183 回答
1

试试这个:

SELECT * FROM Posts INNER JOIN Topics ON idTopic = idTopics  
INNER JOIN Users ON idUserPosts = idUsers
WHERE idTopic = :idTopic
ORDER BY Posts.datePosts DESC
LIMIT :limit_down, :limit_up;

限制必须是应用于查询的最后一件事,因此 order by 需要放在第一位。

于 2012-04-27T12:55:33.227 回答
0

似乎 dateposts 是一个日期字段,那么为什么不试试这个

SELECT * FROM Posts INNER JOIN Topics ON idTopic = idTopics  
INNER JOIN Users ON idUserPosts = idUsers
WHERE idTopic = :idTopic LIMIT :limit_down, :limit_up 
ORDER BY DATE(Posts.datePosts) DESC;
于 2012-04-27T12:57:53.500 回答