在PHP中对select语句进行排序的最佳方法

时间:2021-10-14 22:25:24

Hello can anyone point me in the right direction here. What is the best way to select results from a tabel but show them in a certain way, see below for preferred sorting.

你好,任何人都可以指出我在这里正确的方向。从表格中选择结果但以某种方式显示它们的最佳方法是什么,请参阅下面的首选排序。

10, 9, 8, 6, 5, 4, 3, 2, 1

make that into

把它变成

6, 7, 8, 9, 10

and show in that order?

并按顺序显示?

I have this so far...

到目前为止我有这个...

$result = mysql_query("SELECT * FROM Chat ORDER BY Time DESC LIMIT 5");

4 个解决方案

#1


4  

Assuming that the numbers are not only 1-10, wrap it in a subquery and reorder back again.

假设数字不仅仅是1-10,请将其包装在子查询中并重新排序。

SELECT *
FROM
   (
          SELECT *
          FROM Chat 
          ORDER BY `Time` DESC
          LIMIT 5           -- <=== change this to the number of records you want
   ) a
ORDER BY `TIME`

SQLFiddle Demo

#2


0  

Try the following code

请尝试以下代码

$result = mysql_query("SELECT * FROM Chat ORDER BY Time ASC LIMIT 5,5");

#3


0  

$result = mysql_query("SELECT * FROM Chat ORDER BY Time ASC LIMIT 5,5");

Sort ascending, start at the 6th result, return 5 rows.

按升序排序,从第6个结果开始,返回5行。

#4


0  

query will be like this

查询将是这样的

$result = mysql_query("select * from (SELECT * FROM Chat ORDER BY Time ASC LIMIT 0,5) order by time desc");

it will first take all result in descending order and after that take first 5 rows and make it in ascending order.

它将首先按降序排列所有结果,然后取前5行并按升序排列。

#1


4  

Assuming that the numbers are not only 1-10, wrap it in a subquery and reorder back again.

假设数字不仅仅是1-10,请将其包装在子查询中并重新排序。

SELECT *
FROM
   (
          SELECT *
          FROM Chat 
          ORDER BY `Time` DESC
          LIMIT 5           -- <=== change this to the number of records you want
   ) a
ORDER BY `TIME`

SQLFiddle Demo

#2


0  

Try the following code

请尝试以下代码

$result = mysql_query("SELECT * FROM Chat ORDER BY Time ASC LIMIT 5,5");

#3


0  

$result = mysql_query("SELECT * FROM Chat ORDER BY Time ASC LIMIT 5,5");

Sort ascending, start at the 6th result, return 5 rows.

按升序排序,从第6个结果开始,返回5行。

#4


0  

query will be like this

查询将是这样的

$result = mysql_query("select * from (SELECT * FROM Chat ORDER BY Time ASC LIMIT 0,5) order by time desc");

it will first take all result in descending order and after that take first 5 rows and make it in ascending order.

它将首先按降序排列所有结果,然后取前5行并按升序排列。