我需要帮助从MySQL数据库获取数据。现在我有一个查询给我:
锦标赛ID
锦标赛名称
锦标赛入场费
锦标赛开始和结束日期
对于我注册的锦标赛。现在我希望,在我注册的每个锦标赛中,计算该锦标赛中有多少用户,我在该锦标赛中的积分等。
该信息位于名为'ladder'
的表格中ladder.id
ladder.points
ladder.userFK
ladder.tournamentFK
用于显示我注册的锦标赛的PHP代码:
<?php
include('config.php');
$sql = "SELECT distinct tournaments.idtournament, tournaments.name, tournaments.entryfee, tournaments.start, tournaments.end
from tournaments join ladder
on tournaments.idtournament= ladder.tournamentFK and ladder.userFK=".$_SESSION['userid']."
group by tournaments.idtournament";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()) {
$tournament="<li class='registered' data-id=".$row['idtournament']." data-entryfee=".$row['entryfee']." data-prize=".$tournamentPrize."><span class='name'>".$row['name']."</span><span class='entry-fee'>Entry fee: ".$row['entryfee']."€</span><span class='prize-pool'>Prize pool: €</span><span class='date-end'>".$row['start']."-".$row['end']."</span><span class='btns'><button>Standings</button></span></li>";
echo $tournament;
}
}
$conn->close();
?>
答案 0 :(得分:0)
通常,您可以在查询中组合JOIN,COUNT()和GROUP BY。
答案 1 :(得分:0)
这就是我认为的查询。如果列不正确,请更改列和表名。 未经过测试,但我相信这会让您有一些想法来进行必要的查询
select count(ladder.tournamentId)as userCount,tournaments.name
from
ladder left join tournaments
on ladder.tournamentId = tournaments.id
where ladder.tournamentId in
(
select tournaments.id from
tournaments left join ladder
on ladder.tournamentId = tournaments.id
where ladder.userId='yourId'
) and ladder.userId <> 'yourId'
group by ladder.tournamentId