如何反向排序mysql数据 [英] how to reverse sort mysql data
问题描述
我知道一个奇怪的标题,但我很难过.假设我像这样抓取 20 行:
An odd title I know, but I'm stumped. Say if I grab 20 rows like this:
SELECT * FROM `articles` WHERE date >= UNIX_TIMESTAMP(DATE(NOW() - INTERVAL 30 DAY)) ORDER BY `views` DESC LIMIT 20
然后我想反向显示它们,从最低到最高的视图",我该怎么做?我不能简单地按ASC"排序,因为这给了我错误的结果集.我想要最高的观看次数,然后按照从最低到最高的顺序排列.
I then want to show them in reverse, from lowest to highest "views", how can I do this? I cannot simply order by "ASC" as that gives me the wrong result set. I want the highest views and to then order those from lowest to highest.
推荐答案
采纳 Zerkms 的想法:
taking Zerkms's idea:
Select * FROM (
SELECT * FROM `articles`
WHERE date >= UNIX_TIMESTAMP(DATE(NOW() - INTERVAL 30 DAY))
ORDER BY `views` DESC
LIMIT 20
) as reverse_article
ORDER BY views ASC
会让你双重排序
最高的将被选中(内部选择)
The highest will get picked (inner select)
最低的将首先被选中.
然而
您也可以通过 PHP 反向排序,首先获取您的 fetchALL 并从 count()(这是您的最大大小数组)向下运行计数到 0(这是您的最高数组视图)
you can also reverse sort via PHP by first getting your fetchALL and running the count backward from count() (which is your max size array) down to 0 (which is your highest array view)
随便选
这篇关于如何反向排序mysql数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!