PHPでの並べ替えMySQLのクエリ

六神:

私は順序を(最高最初の)降順でソート異なるユーザのポイント」にしようとしています。しかし、現時点では、クエリは、ユーザーのID(彼らはデータベースに表示される順序)の順に返されています。私は私のコードが間違っているかわからないんだけど?

これは、最初のクエリは、特定のユーザーがであることを何リーグを見て、ユーザは、複数のリーグにすることができる。リーグIDを持つ、私は、ユーザーがリーグのそれぞれにあるものを見るために問い合わせます。それから私は、各ユーザーの合計ポイントがそのリーグ内であるかを問い合わせます。最終的に、私は、各リーグのために、ユーザのランクを取得したいが、現時点でのポイントによって順位へのクエリが動作していません。

画像ショーはどのようにポイントが出てきています。「1635」はログインしているユーザーがポイントである。最初のリーグのために、私は「ランク2」を取得しようとしている表示されます。

ここでは、画像の説明を入力します。

// SQL query to see what leagues user is in
    $query = mysqli_query($con, "SELECT * FROM UserLeague WHERE UserID='$userid'");
    $num = mysqli_num_rows($query);

    if($num == 0) {
      echo 'You are not in any leagues';
      return;
    } else {
      echo '<div class="pleague-table">';
      echo '<div class="pleague-table-header">';
      echo '<p>PRIVATE LEAGUE</p>';
      echo '<p>CURRENT RANK</p>';
      echo '</div>';
    }

    while($leagueid = mysqli_fetch_assoc($query)) {

        $lid = $leagueid['LeagueID'];

        // Get all league info that user is in

        $query2 = mysqli_query($con, "SELECT * FROM League WHERE LeagueID='$lid'");

        // Get all users that is in each league

        $queryposition = mysqli_query($con, "SELECT UserID FROM UserLeague WHERE LeagueID='$lid'");

        while($getpoints = mysqli_fetch_assoc($queryposition)) {

          $uid = $getpoints['UserID'];

          // Get each users points in each league

          $querypoints = mysqli_query($con, "SELECT * FROM Points WHERE UserID='$uid' ORDER BY total DESC");
          while($row = mysqli_fetch_assoc($querypoints)) {
            echo $row['total']. '</br>';
          }


        }



        while($leaguename = mysqli_fetch_assoc($query2)) {
          echo '<div class="league-link">';
          echo $leaguename['Name'];
          echo '<a href="#">Options</a>';
          echo '</div>';
        }

    }
'''
アレクサンダー・ウシャコブ:

UserLeagueからユーザーを選択して、ポイントがそれらを注文するUserLeagueとポイント:あなたは、2つのテーブルを結合しようとしています。このような場合のためにSQLでJOIN構文があります:

SELECT Points.* 
FROM Points 
RIGHT JOIN UserLeague ON Points.UserID=UserLeague.UserID 
WHERE UserLeague.LeagueID=? 
ORDER BY Points.total DESC

おすすめ

転載: http://10.200.1.11:23101/article/api/json?id=387854&siteId=1
おすすめ