Jump to content

Recommended Posts

Hello.

 

I know that I can find the MIN and MAX values of a field by using this:

SELECT MIN(field) AS minfield FROM table
SELECT MAX(field) AS maxfield FROM table

 

But how can I find out the NEXT highest or next lowest number?

 

For example, if my field has numbers like 2, 3, 5, 7, 9. I want to be able to look at the row with number 5 and determine the value of the the next highest and lowest number.  Which in this case would be 7 and 3. BUT I don't need to know exactly what the number is, I just need to know it so I can add or subtract 1 to it.

 

Anybody know how? Thanks.

Link to comment
https://forums.phpfreaks.com/topic/142289-solved-php-mysql-query-help/
Share on other sites

Thanks revraz.

 

So by doing this:

$thisposition= mysql_query("SELECT position FROM table WHERE id = $ID;");
$thisposition = mysql_fetch_array($thisposition);

$allpositions= mysql_query("SELECT position FROM table");
$allpositions = mysql_fetch_array($allpositions);

 

Now I know the position number of the current row id.

 

I also now know the position numbers of all of the other rows.

 

But now, how do I use what I know to find the next position number higher and add 1 to it?

Thanks Void.

 

I'm trying to wrap my head around this but my code aint working

 

$ID = $_GET['id'];
$higherposition= mysql_query("SELECT position FROM table WHERE id = $ID ORDER BY DESC LIMIT 1,1");
$lowerposition= mysql_query("SELECT position FROM table WHERE id = $ID ORDER BY DESC LIMIT 1,1");

echo $higherposition;
echo '<br>';
echo $lowerposition;

 

Nothing is echoing?

??? ??? ??? ??? ???

 

 

 

For example, if my field has numbers like 2, 3, 5, 7, 9. I want to be able to look at the row with number 5 and determine the value of the the next highest.  Which in this case would be 7. BUT I don't need to know exactly what the number is, I just need to know it so I can add or subtract 1 to it and make that number the new value for the current field.

Got it! Thanks people. You all put me on the right path. SOLVED!

 

$ID = $_GET['id'];
$query = mysql_query("SELECT field FROM table WHERE id = $ID;");
$row = mysql_fetch_assoc($query);
$thisfield= $row['field'];
//higher
$query= mysql_query("SELECT field FROM table WHERE field > $thisfield LIMIT 1");
while($row = mysql_fetch_assoc($query)) {


echo '<br>';
echo $row['field];
echo '<br>';


}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.