I have a table that is something like below
column1 | column2 | column3 | column4
43 12 1 132
Obviously, the 'column4' has the highest value. So my goal is to retrieve this column.
Desire results:
column4
132
I have a table that is something like below
column1 | column2 | column3 | column4
43 12 1 132
Obviously, the 'column4' has the highest value. So my goal is to retrieve this column.
Desire results:
column4
132
Try the function GREATEST():
SELECT GREATEST(column1, column2 colum3, column4)
FROM table
If you want the column name:
select greatest(column1, column2, column3, column4) as biggestval,
(case when column1 = greatest(column1, column2, column3, column4) then 'column1'
when column2 = greatest(column1, column2, column3, column4) then 'column2'
when column3 = greatest(column1, column2, column3, column4) then 'column3'
when column4 = greatest(column1, column2, column3, column4) then 'column4'
end) as biggestval_columnname
Note that this will not work if any of the columns have NULL
values.
Try this :
SELECT
CASE
WHEN column1 >= column2 AND column1 >= column3 THEN column1
WHEN column2 >= column1 AND column2 >= column3 THEN column2
WHEN column3 >= column1 AND Date3 >= column2 THEN column3
ELSE column1
END AS TopColumn
There isn't a SQL standard function that makes what you have requested
Stock your value in a array and use function PHP MAX , Try this
$sql = 'SELECT column1, column2, column3, column4 FROM table';
$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error());
$t_number=array();
$i=0;
while($data = mysql_fetch_assoc($req)){
$t_number[$i]=array(
$data['column1'],
$data['column2'],
$data['column3'],
$data['column4']
);
$maxValue=max($t_number[$i]);
echo $maxValue;
$i++;
}