Hi everyone, is it possible to have a calculated field in a MySQL database?
Basically, I want the value of a particular field to be calculated based on values of other fields in a record.
Three columns: “[B]Pld[B]W[B]Percentage
I need the “[B]Percentage
Is this possible? Or is there a way in PHP that I can pull the data from the database, calculate the percentage, and then order it again.
Currently, this is what I am doing.
[code=php]
<?php
$con = mysql_connect(“SERVER”,”USER”,”PASS”);
if (!$con)
{
die(‘Could not connect: ‘ . mysql_error() . ‘<br /><br />Please contact your web developer’);
}
mysql_select_db(“DATABASE”, $con);
$sql = ”
SELECT * FROM tablename WHERE Pld > 0 ORDER BY W DESC , Percentage DESC , PlayerName”;
$result = mysql_query($sql);
if (!$result)
echo mysql_error();
echo “<table class=’tab’>
<tr>
<th class=’team’>Player</th>
<th>Pld</th>
<th>W</th>
<th>L</th>
<th>%</th>
</tr>”;
while($row = mysql_fetch_array($result))
{
echo “<tr>”;
echo “<td class=’team’> ” . $row[‘PlayerName’] . ” (” . $row[‘Team’] . “)</td>”;
echo “<td>” . $row[‘Pld’] . “</td>”;
echo “<td>” . $row[‘W’] . “</td>”;
echo “<td>” . ($row[‘Pld’] – $row[‘W’]) . “</td>”;
echo “<td>” . ($row[‘W’] / $row[‘Pld’] * 100) . “</td>”;
echo “</tr>”;
}
echo “</table>”;
mysql_close($con);
?>
I need to calculate the % for each record, then order the records in the same way as I’ve asked the SQL data to be ordered. I have to manually enter the percentages in the database at the moment to get it to work.