2015-09-04 63 views
1

我在使結果正常工作時遇到問題。我有兩個疑問,一個是把所有俱樂部的信息都放到桌子上,另一個是統計表中每個俱樂部的總人數。從兩張不同表格的總數中得到的總數

問題是總數顯示不正確,在桌子上的最後一個單元格之後,而不是像我打算顯示它一樣。

下面是它的外觀:

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> 

代碼難道這在單個查詢做到嗎?或者我做錯了什麼,它導致它被顯示那樣?

+0

是的,你可以在一個查詢中做到這一點。基本上它是 - * SELECT SUM('members'),'club' FROM'table' GROUP BY'club' * –

回答

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 
+0

非常感謝你,我有了這個想法並嘗試了一個類似的查詢,但在FROM部分犯了一個錯誤的。再次感謝。 – jChris

相關問題