How can the use of COUNT() in MySQL help in calculating the ranking of teams in a league table?
To calculate the ranking of teams in a league table using COUNT() in MySQL, you can count the number of matches won by each team and order them in descending order. This will give you a list of teams ranked by the number of wins they have achieved in the league.
$query = "SELECT team_name, COUNT(*) AS total_wins
FROM matches
WHERE result = 'win'
GROUP BY team_name
ORDER BY total_wins DESC";
$result = mysqli_query($connection, $query);
if(mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo $row['team_name'] . " - Total Wins: " . $row['total_wins'] . "<br>";
}
} else {
echo "No results found.";
}
Keywords
Related Questions
- What are some common formatting errors in PHP code that can lead to issues like incorrect ordering of entries in a guestbook?
- What potential pitfalls should be considered when using loops in PHP to display sequential numbers?
- How can session management be effectively utilized in PHP to remember the state of collapsed menu items for individual users?