2008-10-24 144 views
1

我有一個志願者_2009表,列出了所有的志願者和場地表,列出了志願者可以分配到的場所,他們只被分配到一個。如何根據其他表格計算每個值的計數?

我想要做的是打印出分配給每個場地的志願者人數。

我希望它打印出來是這樣的:

地點的名稱:志願者#

表:volunteers_2009 列:ID,姓名,venue_id

表:場館 列:ID ,venue_name

它們涉及通過volunteers_2009.venue_id = venues.id

這是我的,但它不正常工作。

$sql = "SELECT venues.venue_name as 'Venue', COUNT(volunteers_2009.id) as 'Number Of 
Volunteers' FROM venues ven JOIN volunteers_2009 vol ON 
(venues.id=volunteers_2009.venue_id) GROUP BY venues.venue_name ORDER BY 
venues.venue_name ASC"; 

$result = mysql_query($sql); 

while(list($name,$vols) = mysql_fetch_array($result)) { 
    print '<p>'.$name.': '.$vols.'</p>'; 
} 

回答

2

不是MySQL的人所以這可能是真的錯了,但是當你給你的表的別名,你不那麼需要用這個名字來引用它。

$sql = "SELECT ven.venue_name as 'Venue', COUNT(vol.id) as 'Number Of 
Volunteers' FROM venues ven JOIN volunteers_2009 vol ON 
(ven.id=vol.venue_id) GROUP BY ven.venue_name ORDER BY ven.venue_name ASC"; 
0
$query = "SELECT ven.venue_name AS 'Venue', count(*) AS 'Number of venues' 
     FROM volunteers_2009 AS vol, venues AS ven WHERE vol.venue_id = ven.id 
     GROUP BY ven.venue_name"; 
相關問題