Author Topic: get last 10 rows in database but order opposite  (Read 860 times)

0 Members and 1 Guest are viewing this topic.

Offline jwk811Topic starter

  • Devotee
  • Posts: 714
    • View Profile
get last 10 rows in database but order opposite
« on: June 14, 2010, 01:58:17 PM »
i did this
Code: [Select]
$sql="SELECT * FROM chat_messages ORDER BY message_id DESC LIMIT 10";

but i want those last 10 rows that im taking to be ordered the opposite way. if i use ASC it gives me the first 10 rows in the table

Offline jwk811Topic starter

  • Devotee
  • Posts: 714
    • View Profile
Re: get last 10 rows in database but order opposite
« Reply #1 on: June 14, 2010, 02:06:02 PM »
nevermind i got it. i did this

Code: [Select]
$sql = "SELECT * FROM chat_messages";
$result = mysql_query($sql);
$num = dbNumRows($result);

$last = 10;
$num = $num - $last;

$sql="SELECT * FROM chat_messages ORDER BY message_id ASC LIMIT $last,$num";

Offline Psycho

  • Guru
  • Freak!
  • *
  • Posts: 7,751
    • View Profile
Re: get last 10 rows in database but order opposite
« Reply #2 on: June 14, 2010, 02:15:47 PM »
No, that is inefficient. You only need to do a single query. The exact same question was asked just a couple days ago and a solution provided:
http://www.phpfreaks.com/forums/index.php/topic,300778.msg1423631.html#msg1423631

Code: [Select]
SELECT *
FROM (SELECT * FROM chat_messages ORDER BY message_id DESC LIMIT 10) as last10
ORDER BY last10.message_id ASC
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

Offline fenway

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Mind Boggling!'
  • *
  • Posts: 15,443
  • Gender: Male
    • View Profile
Re: get last 10 rows in database but order opposite
« Reply #3 on: June 15, 2010, 08:56:49 AM »
nevermind i got it. i did this

Code: [Select]
$sql = "SELECT * FROM chat_messages";
$result = mysql_query($sql);
[/quote]

Yeah, horrible idea -- get back every single record just to get a count?
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.