总计来自两个不同表的计数

时间:2015-09-04 20:32:20

标签: php mysqli count html-table

我在使结果表现正常方面遇到了问题。我有两个问题,一个用于获取俱乐部的所有信息,另一个用于统计每个俱乐部的所有成员。

问题是,在桌子上的最后一个单元格之后,总数不正确地显示,而不是像我打算显示它一样并排显示。

以下是它的外观:

table table

以下是我的疑问:

表格

$sql = "SELECT id, nombreClub, liderVoluntario, region, municipio, oficinaLoc
FROM club4h
ORDER BY region, oficinaLoc";

总计

    $sql = "SELECT count(soc.nombreClub)
            FROM socios  as soc
            RIGHT OUTER JOIN club4h as club
            ON soc.nombreClub = club.nombreClub
            GROUP BY club.id";

以及如何显示表格的代码

<table>
  <?php 
$name = '';
$filler = '';


echo '<tr>
<th>Regi&oacute;n</th>
<th>Unidad Program&aacute;tica</th>
<th>Nombre Club</th>
<th>Lider Voluntario</th>
<th>Localizaci&oacute;n Club</th>
<th>Total Socios</th>
</tr>';

foreach($result as $key=>$row){
echo'<tr>
<td>'.ucfirst($row['region']).'</td>';


if(($row['id']) !=$filler) echo 
'<td>'.ucfirst($row['oficinaLoc']).'</td>';
$filler = $row['id'];

echo '<td>'.ucfirst($row['nombreClub']).'</td>
<td>'.ucfirst($row['liderVoluntario']).'</td>
<td>'.ucfirst($row['municipio']).'</td>';
}

include_once 'sociocount.php';
foreach($memberSearch as $total){
echo '<td>'.$memberSearch[$i][0].'</td>
</tr>';
$i++;
}
?>
</table>

这可以在一个查询中完成吗?或者我做错了什么导致它显示出来?

1 个答案:

答案 0 :(得分:1)

您可以在一个查询中选择所有内容,例如:

SELECT
    id,
    nombreClub,
    liderVoluntario,
    region,
    municipio,
    oficinaLoc,
    (select count(*) from socios as soc where soc.nombreClub=c.nombreClub) as memberCnt
FROM club4h as c
ORDER BY region, oficinaLoc
相关问题