如何反向排序mysql数据

时间:2014-03-30 22:35:53

标签: php mysql

我知道一个奇怪的标题,但我很难过。如果我像这样抓20行:

SELECT * FROM `articles` WHERE date >= UNIX_TIMESTAMP(DATE(NOW() - INTERVAL 30 DAY)) ORDER BY `views` DESC LIMIT 20

然后我想反过来显示它们,从最低到最高的“视图”,我该怎么做?我不能简单地通过“ASC”订购,因为这给了我错误的结果集。我想要最高的观点,然后从低到高排序。

4 个答案:

答案 0 :(得分:1)

采取Zerkms的想法:

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

会让你加倍排序

  1. 最高的将被选中(内部选择)

  2. 最低价将首先被选中。

  3. 无论其

    你也可以通过PHP反向排序,首先获取你的fetchALL并从count()(这是你的最大大小数组)向下运行计数到0(这是你的最高数组视图)

    选择

答案 1 :(得分:0)

您使用子查询。子查询选择正确的20行,外部查询然后根据您的最终标准对它们进行排序:

SELECT a.*
FROM (SELECT a.*
      FROM articles a
      WHERE date >= UNIX_TIMESTAMP(DATE(NOW() - INTERVAL 30 DAY))
      ORDER BY views DESC
      LIMIT 20
     ) a
ORDER BY views ASC;

答案 2 :(得分:0)

这应该有效

SELECT * FROM (SELECT * FROM `articles` WHERE date >= UNIX_TIMESTAMP(DATE(NOW() - INTERVAL 30 DAY)) ORDER BY `views` DESC LIMIT 20) t ORDER BY `views` ASC

答案 3 :(得分:0)

MySQL查询:

(SELECT * FROM `articles` WHERE date >= UNIX_TIMESTAMP(DATE(NOW() - INTERVAL 30 DAY)) ORDER BY `views` DESC LIMIT 20) ORDER by `views` ASC;