比较PHP中的两个动态表

时间:2011-10-05 14:51:36

标签: php mysql

所以我有一个由一个mysql数据库填充的联赛表,我想要做的是,旁边有一个箭头,说明自上次比赛以来他们是否已经上升或下降,这是对于登录的成员来说,这很容易做到这一点,但是如何通过表中的每个成员实现这一点?我可以通过下面的查询找出它们在比赛之前的排名,只需再次运行下面的查询,但将case语句更改为Case当r.track_id ='$ chosenntrack' -1 ,这将给我以前的比赛的积分,但我怎么能比较这个表与目前的排名???如果每个成员上下移动,箭头会向上或向下移动。

任何帮助都会很棒,非常感谢。

$result = mysql_query(" SELECT m.member_id, m.teamname, 
    Sum(Case When r.track_id = '$chosentrack' AND r.track_id >= l.start_race 
    Then total_points Else 0 End) LastRacePoints,
    Sum(Case When r.track_id <= '$chosentrack' AND r.track_id >= l.start_race
    Then total_points Else 0 End) TotalPoints 
    FROM members m
    Join members_leagues l
    On l.member_id = m.member_id  
    Join member_results r
    On r.member_id = m.member_id
    Where l.league_id = '$chosenleague'
    Group By m.member_id
    Order By TotalPoints Desc, LastRacePoints DESC, m.teamname Desc ") 
    or die ("Error - could not display league");

    $i = 0;
    $found = false;
    $team_position = 0;

  while (!$found && $row = mysql_fetch_row($result)){
    $i++;
    if ($row[0] == $session_id){
    $found = true;
    $team_position = $i;
    }
    }

    $rowsPerPage = 6;

    $pageNum = ceil($team_position/$rowsPerPage);

    if(isset($_GET['page'])){
        $chosenpage = mysql_real_escape_string($_GET['page']);
        $pageNum = $chosenpage;
    }

    $offset = ($pageNum - 1) * $rowsPerPage;
    $counter = $offset + 1;

    $result = mysql_query("SELECT m.member_id, m.teamname, 
    Sum(Case When r.track_id = '$chosentrack' AND r.track_id >= l.start_race 
    Then total_points Else 0 End) LastRacePoints,
    Sum(Case When r.track_id <= '$chosentrack' AND r.track_id >= l.start_race
    Then total_points Else 0 End) TotalPoints 
    FROM members m
    Join members_leagues l
    On l.member_id = m.member_id  
    Join member_results r
    On r.member_id = m.member_id
    Where l.league_id = '$chosenleague'
    Group By m.member_id
    Order By TotalPoints Desc, LastRacePoints DESC, m.teamname Desc " . "
    LIMIT $offset, $rowsPerPage") 
    or die ("Error - could not display league");

        echo "<table id=\"subleaguetable\" cellpadding=\"0\" cellspacing=\"0\">";
        echo "<tr><th width=\"30px\" style=\"text-align: center;\">Pos</th>";
        echo "<th width=\"200px\" style=\"background-color: #f13740;\">Team</th>";
        echo "<th width=\"200px\" style=\"text-align: center; background-color: #f13740;\">Points/Overall</th>";
        echo "<th width=\"100px\" style=\"text-align: center; background-color: #f13740;\">Points/Last Race</th>";
        echo "</tr>";

    while($row = mysql_fetch_assoc($result)){

    echo "<tr><td style=\"text-align:center;\">";
    echo $counter;
    echo "</td>";
    if($row['member_id'] == $session_id){
    echo "<td style=\"background-color:#e4212a; color:#FF9912;\">";
    echo $row['teamname'];
    echo "</td>";
    }else{
    echo "<td style=\"background-color:#e4212a;\">";
    echo $row['teamname'];
    echo "</td>";
    }
    echo"<td style=\" background-color:#e4212a; text-align:center;\">";
    echo $row['TotalPoints'];
    echo "</td><td style=\" background-color:#e4212a; border-bottom:solid 1px #ca0811;  text-align:center;\">";
    echo $row['LastRacePoints'];
    echo "</td></tr>";
    }
    $counter++;
    }
    echo "</table>";

1 个答案:

答案 0 :(得分:0)

您在查询中尝试过INTERSECT吗?