2012-03-03 52 views
0

我試圖組合一個命令來從不同的表中提取數據,並使用count()函數,但我無法正確理解它。來自多個表的數據並在選擇中使用count()

我需要的名稱和服務,並計數的說明()多少次服務在X年完成 我的表:

服務= id_service,描述,名稱 歷史= ID,id_service (這是服務的外鍵),一年

任何幫助,將不勝感激:d 我想是這樣的,但它不工作:S

select X.description, X.name, Z.year 
from Services X 
INNER JOIN history Z 
ON Z.id_service = x.id_service AND Z.year= 2010; 

回答

0

關閉。您需要將COUNT()命令添加到您計算領域,並添加等領域的GROUP BY:

select X.description, X.name, Z.year, count(z.id_service) 
from Services X 
INNER JOIN history Z 
ON Z.id_service = x.id_service -- AND Z.year= 2010 leave this off for a count from all years 
GROUP BY X.description, X.name, Z.year 
+0

哎太感謝你了:D嚴重的是,有一天,我希望我能像你們知道這麼多:D – itsmedavid 2012-03-03 02:46:39