我有以下代码,它根据 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_ID
和 Tipper_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 个选择:
- 在一段时间内,对每个结果执行另一个查询(根据结果的数量,性能可能不太好)
- 创建一个 View ,其中结果按
Tipper_Name
(或您需要的任何内容)分组,并使用您刚刚创建的 View 更改查询,添加 INNER JOIN。即使这个解决方案稍微复杂一点,但速度更快!
关于php - Foreach 与 php 和 mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21386834/