php - Foreach 与 php 和 mysql

标签 php mysql

我有以下代码,它根据 Resultat_ID 的设置提供行的profit 结果。

$result = mysql_query('SELECT Indsats, Odds, Resultat_ID, Kamp FROM spil'); 
$row = mysql_fetch_assoc($result);
echo " Gevinster: "; 
$Indsats = $row['Indsats'];
$Odds = str_replace(",", ".", $row['Odds']);
$sum = 0;

while($row = mysql_fetch_array( $result ))
{
    $Indsats = $row['Indsats'];
    $Odds = str_replace(",", ".", $row['Odds']);
    if ($row['Resultat_ID'] == 1)      //Win
    {
       $sum += $Indsats * ($Odds-1);
    }
    elseif ($row['Resultat_ID'] == 2)  //Loss
    {
       $sum += $Indsats * -1;
    }
    elseif ($row['Resultat_ID'] == 3)  //HalfWin
    {
       $sum += $Indsats * ($Odds-1) * 0.5;
    }
    elseif ($row['Resultat_ID'] == 4)  //HalfLoose
    {
       $sum += $Indsats * -0.5;
    }
}

echo $sum;

我还有一个名为 Tipper_ID 的列,其中包含每个小费者的编号,以及一个包含 Tipper_IDTipper_Name 的表。

除了上面的总profit结果之外,我还想获得每个小费者的profit结果,所以基本上对“all”和每个Tipper_Name运行上面的部分Tipper-table 并获取每个部分的利润结果。

我该怎么做?

丹麦语/英语 Translations :

Gevinster = Gains<br>
Indsats = Effort<br>
Odds = Odds<br>
Resultat = Results<br>
Kamp = Match/Game<br>
Spil = Games<br>
Tipper = Tips

最佳答案

您有 2 个选择:

  1. 在一段时间内,对每个结果执行另一个查询(根据结果的数量,性能可能不太好)
  2. 创建一个 View ,其中结果按 Tipper_Name(或您需要的任何内容)分组,并使用您刚刚创建的 View 更改查询,添加 INNER JOIN。即使这个解决方案稍微复杂一点,但速度更快!

关于php - Foreach 与 php 和 mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21386834/

相关文章:

php - 使用AJAX从MySQL中获取在PHP中创建的表,不带参数

javascript - 如何在html中显示ajax

.net - CMS 类似于 Umbraco 但在 PHP 中?

php - 如何将Word文档中的问题和答案批量上传到MYSQL数据库?

php - 通过 TCP 进行连续数据处理

php - 获取日期差异

php - DOMPDF,给我带来错误

java - Openshift:在应用程序之间共享 MySQL cartrige

mysql - 使用 mysql 表搜索表?

mysql - 如何在 MySQL 中测试子查询并返回第一个非空结果?