i am trying to run an SQL Query to select all rows from a table and then display the column with the lowest value for each row.
for example, lets start with row1...
columns1, column2, column3, column4
the lowest value for this row is in column3
so i need to echo the column3
value then on row2
, column4
has the lowest value so column4
should be displayed for this row.
i did try this code:
$sql="SELECT * from callplanmeta ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
while($result=mysql_fetch_array($rs))
{
$sql2="SELECT *, MIN(NULLIF(".$result["callplanname"].",0)) as number2 from callplandata ";
echo $sql2;
$rs2=mysql_query($sql2,$conn) or die(mysql_error());
while($result2=mysql_fetch_array($rs2))
{
echo $result2["description"].' - '.number_format($result2["number2"],2).'<br><br>';
}
}
however i then realised that it was doing it the wrong way round and only showing the lowest value for 1 row
the column names in the callplandata
table match the column callplanname
in the callplanmeta
table
the columns in the callplandata
table are dynamic so they are changing all the time and more are constantly being added