简体   繁体   English

比较2个While循环中的值(PHP MYSQL)

[英]Compare Values From 2 While Loops (PHP MYSQL)

I'm trying to create all time standings for head to head matchups in a fantasy football league. 我正在尝试创建一个幻想足球联赛中对决比赛的所有时间排名。 I'm using two separate queries and grabbing the data with two separate while loops. 我正在使用两个单独的查询,并使用两个单独的while循环获取数据。 I was hoping I could compare the two with an IF statement, but no-go. 我希望我可以将两者与IF语句进行比较,但不行。

My queries are as follows: 我的查询如下:

$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);

My PHP: 我的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++;
}
}
}

The function works for $row6, but for $row5 it's grabbing the same number over and over rather than cycling through the array. 该函数适用于$ row6,但对于$ row5,它会反复获取相同的数字,而不是循环遍历数组。

Here is sample data: 这是示例数据:

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

I need to add up total points for Dustin in week 1 and compare them to total points for Brandon in week 1. And indicate a win for the winning team. 我需要将第一周的达斯汀总分加起来,并将其与第一周的布兰登的总分进行比较,并指出获胜球队的胜利。

The query below will return the total # of wins for dustin, lee, and the total # of ties. 下面的查询将返回布丁,李先生和领带总数的总数。 Where a win is defined by scoring more points than your opponent in the same week. 获胜的定义是在同一周获得比对手更多的积分。

The query assumes that both players have at least some points for every week. 该查询假设两个玩家每周都有至少一些积分。 If this isn't the case you'll have to convert from an inner join to a full join. 如果不是这种情况,则必须从内部联接转换为完全联接。

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

I'm not real sharp on the ins and outs of complex mysql queries, but this is similar to what i use to calculate votes in a voting system i've been designing. 我对复杂的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++;
    }
}

It may not work for your situation since i use it with COUNT instead of SUM, but it might work. 它可能不适用于您的情况,因为我将其与COUNT而不是SUM一起使用,但它可能会起作用。 if it doesn't let me know and i'll try something else. 如果没有让我知道,我会再尝试其他方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM