MySQL: 用复杂的方程从多个表中创建计算列

MySQL: Make calculated column from multiple tables with complicated equation

我在 PHP 中写了一个公式,它使用来自不同 table 的字段来计算百分比。如果可能的话,我想尝试完成的是将其翻译成 SQL。

我知道我可以使用 SET 创建局部变量,然后 SELECT 该变量显示在我的最终 table 中,但是将这些变量设置为方程式给我带来了一些麻烦。

这是 PHP。我试图让它尽可能简单而不令人困惑。

/*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);
}

并且 MySQL 我已经开始了。我的攻击计划是使用列名将公式分配给变量,包括检查零除法。 运行 通过 SQL Maestro,给出无效 SET 令牌的错误。我尝试用双引号将整个内容括起来(并检查了@variable $variable, =, := all 没有运气)并检查了几个来源,我的 SET 命令应该是正确的。我的私钥和外键设置正确。

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

我要查找的最终结果是显示我正在选择的所有单个字段以及计算字段。我研究了 PROCEDURE,我认为这可能是有益的,但我不确定如何将其完全纳入。

我认为 SET 仅适用于 select 查询:How to set variable from a SQL query?

此外,我假设表 1-4 只包含一行。否则,SET 不起作用。

SET @a = (SELECT 
           (((table1.PU*60) - table1.PD - table3.UPD) / (table1.PU*60)) 
          FROM table1, table3);
SET @b = (SELECT 
           (table1.GP + table2.S + table4.R)/(((table1.PU*60) - table1.PD - table3.UPD)*(table1.CT/60)) 
          FROM table1, table2, table3, table4);
SET @c = (SELECT 
           table1.GP/(table1.GP + table2.S + table4.R) 
          FROM table1, table2, table4);
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.UPD,
    table4.R,
    @complete // or perhaps (SELECT @complete as complete)
FROM
  table1, table2, table3, table4

您不需要为此设置 SET。这是一个简单的 SELECT 进行所有计算:

SELECT
    table1.PU * 60 AS PU,
    table1.PD AS PD,
    table3.UPD AS UPD,
    table1.GP AS GP,
    table2.S AS S,
    table4.R AS R,
    table1.CT/60 AS PR,
    (table1.PU * 60) - table1.PD - table3.UPD AS TRT,
    (table1.PU * 60 - table1.PD - table3.UPD) * (table1.CT/60) AS PT,
    table1.GP + table2.S + table4.R AS TP,
    ((table1.PU * 60) - table1.PD - table3.UPD) / (table1.PU * 60) AS a,
    (table1.GP + table2.S + table4.R) / ((table1.PU * 60 - table1.PD - table3.UPD) * (table1.CT/60)) AS b,
    table1.GP / (table1.GP + table2.S + table4.R) AS c,
    100 * (((table1.PU * 60) - table1.PD - table3.UPD) / (table1.PU * 60)) * ((table1.GP + table2.S + table4.R) / ((table1.PU * 60 - table1.PD - table3.UPD) * (table1.CT/60))) * (table1.GP / (table1.GP + table2.S + table4.R)) AS complete
FROM
    table1,
    table2,
    table3,
    table4

假设您所有的表格都只包含一行。否则你需要指定一些 JOIN 条件。