我正试图在梦幻足球联赛中创造所有时间积分榜。我正在使用两个单独的查询并使用两个单独的while循环来获取数据。我希望我可以将这两者与IF声明进行比较,但不要去。
我的查询如下:
$query5 = "SELECT *, SUM(POINTS) ";
$query5 .= " FROM SCHEDULE";
$query5 .= " WHERE owner = 'DUSTIN'";
$query5 .= " AND OPPONENT = 'LEE'";
$query5 .= " GROUP BY WEEK ";
$result5 = mysqli_query($con, $query5);
$query6 = "SELECT *, SUM(POINTS) ";
$query6 .= " FROM SCHEDULE";
$query6 .= " WHERE owner = 'LEE'";
$query6 .= " AND OPPONENT = 'DUSTIN'";
$query6 .= " GROUP BY WEEK ";
$result6 = mysqli_query($con, $query6);
我的PHP:
while($row5 = mysqli_fetch_assoc($result5)){$row5 ["SUM(POINTS)"];
while($row6 = mysqli_fetch_assoc($result6)){$row6 ["SUM(POINTS)"];
if ( $row5 ["SUM(POINTS)"] > $row6 ["SUM(POINTS)"] ){
$win++;
}
else {
$loss++;
}
}
}
该函数适用于$ row6,但对于$ row5,它会反复抓取相同的数字,而不是在数组中循环。
以下是示例数据:
YEAR WEEK OWNER OPPONENT POSITION PLAYER POINTS
2008 1 Dustin Brandon RB Chris Johnson, Ten RB 19.7
2008 1 Dustin Brandon QB Ben Roethlisberger, Pit QB 13.1
2008 1 DUSTIN BRANDON D/ST Titans D/ST, Ten D/ST 17
2008 1 DUSTIN BRANDON WR Roddy White, Atl WR 5.4
2008 1 DUSTIN BRANDON RB Laurence Maroney, NE RB 6.1
2008 1 BRANDON DUSTIN RB Adrian Peterson, Min RB 21.4
2008 1 BRANDON DUSTIN RB Edgerrin James, Ari RB 15
2008 1 BRANDON DUSTIN WR Larry Fitzgerald, Ari WR 9.1
2008 1 BRANDON DUSTIN k Joe Nedney, SF K 4.5
2008 1 BRANDON DUSTIN WR Chris Chambers, SD WR 11.4
我需要在第1周将Dustin的积分加起来,然后将它们与第1周的Brandon积分进行比较。并表明获胜团队的胜利。
答案 0 :(得分:1)
下面的查询将返回dustin,lee和总关系数#的总胜数。通过在同一周内获得比对手更多的分数来定义胜利。
该查询假定两个玩家每周至少有一些积分。如果不是这种情况,则必须从内部联接转换为完全联接。
SELECT
COUNT(CASE WHEN t1.points > t2.points THEN 1 END) dustin_win_count,
COUNT(CASE WHEN t2.points > t1.points THEN 1 END) lee_win_count,
COUNT(CASE WHEN t2.points = t1.points THEN 1 END) tie_count
FROM
(SELECT WEEK, SUM(POINTS) points
FROM SCHEDULE WHERE
OWNER = 'DUSTIN'
AND OPPONENT = 'LEE'
GROUP BY WEEK) t1
JOIN (SELECT WEEK, SUM(POINTS) points
FROM SCHEDULE WHERE
OWNER = 'LEE'
AND OPPONENT = 'DUSTIN'
GROUP BY WEEK) t2 ON t1.WEEK = t2.WEEK
答案 1 :(得分:0)
我对复杂的mysql查询的细节并不是很清楚,但这与我在设计投票系统中用来计算投票的情况类似。
$query = "SELECT SUM(POINTS)(CASE WHEN owner = 'DUSTIN' AND opponent = 'LEE' THEN 1 END) dustin,
SUM(POINTS)(CASE WHEN owner = 'LEE' AND opponent = 'DUSTIN' THEN 1 END)lee, FROM SCHEDULE GROUP BY WEEK;";
while($row = mysql_fetch_assoc($query))
{
if($row['dustin'] > $row['lee'])
{
$win++;
}else{
$loss++;
}
}
它可能不适用于您的情况,因为我使用COUNT
而不是SUM,但它可能会起作用。如果它不让我知道,我会尝试别的。