I wrote a formula in PHP that uses fields from different tables to calculate percentage. What i want to try and accomplish is translating it into SQL, if it's possible.
I understand that i can use SET to make local variables, then SELECT that variable to display in my final table, but setting those variables as equations is giving me some hassle.
Here's the PHP. I tried to make it as simple as possible without being confusing.
/*Setting fields to variables for ease*/
$PU = {table1.PU}*60;
$PD = {table1.PD};
$UD = {table3.UD};
$GP = {table1.GP};
$S = {table2.S};
$R = {table4.R};
$PR = {table1.CT}/60;
/*Combining variables*/
$TRT = $PU - $PD - $UD;
$PT = $TRT * $PR;
$TP = $GP + $S + $R;
/*Checking and correcting for zero division plus final calculation*/
if ($PU == 0 || $PT == 0 || $TP == 0){
{complete} = "Error!";
}
else {
$a = $TRT/$PU;
$b = $TP/$PT;
$c = $GP/$TP;
{complete} = 100 * ($a * $b * $c);
}
And the MySQL i have started. My plan of attack was to assign the formulas to variables using the column names, including checking for zero-division. Running this through SQL Maestro, giving the error on invalid SET token. I've tried wrapping the whole thing in double quotes (and checked @variable $variable, =, := all with no luck) and checked several sources, my SET command should be correct. My private keys and foreign keys are set correctly.
SET @a = (((table1.PU*60) - table1.PD - table3.UD) / (table1.PU*60));
SET @b = (table1.GP + table2.S + table4.R)/(((table1.PU*60) - table1.PD - table3.UPD)*(table1.CT/60));
SET @c = table1.GP/(table1.GP + table2.S + table4.R);
SET @complete = 100*(@a*@b*@c);
IF(table1.PU = 0, 'Error!',@a),
IF(table1.CT = 0, 'Error!',@p),
SELECT
table1.PU,
table1.PD,
table1.CT,
table2.S,
table3.UD,
table4.R,
@complete
FROM
table1
LEFT JOIN table2
LEFT JOIN table3
LEFT JOIN table4
The final result i'm looking for is to display all the individual fields i'm selecting, as well as the calculated field. I've looked into PROCEDURE and i think that might be beneficial but i am unsure of how to incorporate it fully.