user1717236
user1717236

Reputation: 11

Best way to sort a select statement in PHP

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");

Upvotes: 1

Views: 105

Answers (4)

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

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.

Upvotes: 0

John Woo
John Woo

Reputation: 263693

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

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

Upvotes: 4

jimp
jimp

Reputation: 17487

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

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

Upvotes: 0

iLaYa  ツ
iLaYa ツ

Reputation: 3997

Try the following code

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

Upvotes: 0

Related Questions