两个不同表的总数
Total from the count of two different tables
我在让我的结果正常运行时遇到了问题。我有两个查询,一个是将俱乐部的所有信息输入 table,另一个是计算 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
的代码
<table>
<?php
$name = '';
$filler = '';
echo '<tr>
<th>Región</th>
<th>Unidad Programática</th>
<th>Nombre Club</th>
<th>Lider Voluntario</th>
<th>Localizació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>
这可以在单个查询中完成吗?还是我做错了什么导致它显示成那样?
您可以 select 一次查询中的所有内容,示例如下:
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
我在让我的结果正常运行时遇到了问题。我有两个查询,一个是将俱乐部的所有信息输入 table,另一个是计算 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
的代码<table>
<?php
$name = '';
$filler = '';
echo '<tr>
<th>Región</th>
<th>Unidad Programática</th>
<th>Nombre Club</th>
<th>Lider Voluntario</th>
<th>Localizació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>
这可以在单个查询中完成吗?还是我做错了什么导致它显示成那样?
您可以 select 一次查询中的所有内容,示例如下:
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