/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] MySQL Calculated field

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]“, “[B]W[/B]“, “[B]Percentage[/B]

I need the “[B]Percentage[/B]” field to be automatically calculate from the other two fields i.e. W/Pld * 100, to give me the percentage of wins.

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’>&nbsp;” . $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);
?>
[/code]

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.

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@TheBearMayJun 08.2009 — Might try something like:

<i>
</i>SELECT *, W/Pld*100 as Percentage FROM tablename WHERE Pld &gt; 0 ORDER BY W DESC , Percentage DESC , PlayerName
Copy linkTweet thisAlerts:
@WelshsteveauthorJun 08.2009 — Thanks, that works ? How do I round down to two decimal places from that?

[B][U]UPDATE[/U][/B]

Don't worry, I got it ?

[code=php]
echo "<td>" . round($row['Perc'],2) . "&#37;</td>";
[/code]
Copy linkTweet thisAlerts:
@DasherJun 11.2009 — Not a big deal but you can have the database do the rounding too. Not sure which is faster.
Copy linkTweet thisAlerts:
@WelshsteveauthorJun 11.2009 — Not a big deal but you can have the database do the rounding too. Not sure which is faster.[/QUOTE]

Yes, when I had the percentage field in the database I had it as DECIMAL(5,2), but the whole idea of me doing this excercise is so that I don't have to have that in there. ?
Copy linkTweet thisAlerts:
@b4webJun 15.2009 — Hi. I know you have this resolved, but based on what you said, I think that you want an automatic update whenever the row gets updated. You may want to do some research into your MySQL's procedures and trigger capabilities. I used Procedures and Triggers in Oracle databases years ago. The procedures were stored on the database.
×

Success!

Help @Welshsteve spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...