gpt4 book ai didi

php - 按日期分组,但显示所有行

转载 作者:太空宇宙 更新时间:2023-11-03 12:34:42 25 4
gpt4 key购买 nike

通常当我将东西组合在一起时,显然是为了将重复项组合在一起,但我正在制作一个获胜者页面,显示一堆累积奖金的获胜者,一些累积奖金有 1 个获胜者,而其他有 3 个,使用 GROUP BY date 将在只有 1 位获胜者的情况下工作,但在应该有 3 位获胜者时只会显示 1 位获胜者。

这是我的代码

$result = $db->query("SELECT * FROM r_winners GROUP BY date ORDER BY date DESC");
while($rows = $result->fetch_assoc()){
print"<table class='cashout' style='width:100%;'>
<th colspan='3'>".$rows['jackpot_name']." Winners</th>
<tr>
<td style='width:33%;'>".$rows['winner']."</td><td style='width:33%;'>$".$rows['amount']."</td><td style='width:33%;'>".date("m/d/Y", $rows['date'])."</td></tr>
</tr></table>";
}

打印出这样的表格

---------------------------------------
Daily Jackpot Winners
Username| $5.00 | 12/5/2012 // This table is right, because there is only 1 winner
---------------------------------------

因为只有 1 个获胜者,所以 GROUP BY 在这里真的没有影响

这是一张多位获奖者的表格

---------------------------------------
Monthly Jackpot Winners
Username | $5.00 | 12/5/2012 // This table is wrong, because there should be 3 winners
---------------------------------------

它需要看起来像这样

---------------------------------------
Monthly Jackpot Winners
Username | $5.00 | 12/5/2012
Username2 | $2.00 | 12/5/2012
Username3 | $1.00 | 12/5/2012
---------------------------------------

我怎样才能做到这一点?

编辑:这也许可以更好地解释这一点 https://gist.github.com/4221167

最佳答案

根本不要使用GROUP BY。您已经在使用 ORDER BY,它将把您的日期放入逻辑“分组”中。

针对您关于如何将结果输出到一张表中的评论,以下是您需要如何修改代码。

$result = $db->query("SELECT * FROM r_winners ORDER BY date DESC");
print "<table class='cashout' style='width:100%;'>";
print "<th colspan='3'>".$rows['jackpot_name']." Winners</th>";
while($rows = $result->fetch_assoc()){
print "<tr><td style='width:33%;'>".$rows['winner']."</td><td style='width:33%;'>$".$rows['amount']."</td><td style='width:33%;'>".date("m/d/Y", $rows['date'])."</td></tr>";
}
print "</table>";

关于php - 按日期分组,但显示所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13735400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com