Jump to content

MYSQL: Finding highest column value from a particular row


andrewgarn

Recommended Posts

Ok so if I have a table set out like this

 

id

number 1

number 2

number 3

 

I'd want to locate data from the row where id = 3, but retrieve information from the column with the highest value in it, ie number 1, 2 or 3.

 

Is this possible? or do i need to retrieve all the data then use a set of nested ifs to find the highest?

<?php

$sql = mysql_query("select * from table where username = 'user1'")
while($r=mysql_fetch_array($sql)) {
     $var1 = $r["var1"];
     $var2 = $r["var2"];
     $var3 = $r["var3"];
}
?>

 

Not sure how to insert value + variable name into array

 

then how to sort into order, then retrieve the 3 highest?

 

something to do with array compact? I dont really use arrays much

I really think those who can't be bothered to normalize their data deserve to struggle, but here

 

$sql = "SELECT a.number1, a.number2, a.number3 
        FROM andrewgarn a
        WHERE id = 1";
$res = mysql_query($sql);
$row = mysql_fetch_assoc($res);
$hival = max($row);                            // find highest value
$colname = array_search($hival, $row);         // find it's column name

echo "$hival : $colname";

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.