Jump to content

ORDER BY desc limit 1 help


ananaz

Recommended Posts

Hello people. I have a field named id (INT NOT NULL auto_increment) and i want to retrieve numbers from it. But i want to retrieve numbers after how big they are, not just the biggest. For example I want to retrieve the 14th biggest id in the rows. This is my script how to get biggest. Help me please xD

 

<?php

$con = mysql_connect("localhost","*****","*********");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

 

mysql_select_db("********", $con);

 

$result = mysql_query("SELECT id FROM stuff ORDER BY id desc limit 1;");

 

$stor = $result[0];

 

  echo $stor;

 

 

mysql_close($con);

?>

Link to comment
https://forums.phpfreaks.com/topic/222532-order-by-desc-limit-1-help/
Share on other sites

Ok if this is the tables

 

Table

 

  id    name          age

 

  3    ananaz        21

  2    karl              12

  1    chuck norris  50

 

I want to be able to pick out the for example second biggest id in this case is 2.

 

 

 

Table2

 

 

  id    name          age

 

  4    newbie        6

  3    ananaz        21

  2    karl              12

  1    chuck norris  50

 

and in this case i want it to retrieve 3.

You have the correct query, you just need to alter the value after limit.  Relating to your first example you would use:

ORDER BY id DESC LIMIT 1,1

 

This means, after you order by 'id', it will go to the first row and select the first one after it.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.