2010-12-13 48 views
2

我有一個Mysql表,它結合了三個表。該表存儲了三個外鍵的每個組合的一些統計數據。Mysql查詢合計一個表中的列,其中另一個兩列合併在一起時是唯一的

例如:

1)一種文件表

columns-

id - primary key 
name 
number_of_lines_of_text - the stats field 

2)表文件夾具有一個與文件表中的許多關係

專欄-

id - primary key 
name 

3)Folder_Groups表具有許多與文件夾表

columns-

id - primary key 
name 

最後的組合表一對多關係ordered_file_stats

columns-

folder_group_id - foreign key from 3 above(can repeat multiple times) 
folder_id - foreign key from from 2 above(can repeat muliple times) 
file_id - foreign key from 1 above, unique but only a subset of the records from 1 

,我需要選擇,總結了文件表的number_of_lines_of_text領域的folder_group_id並在ordered_file_statsfolder_id每個獨特組合。

這可能看起來像

folder_group_id folder_id file_id 
    1    2   1 
    1    2   2 
    1    3   9 
    2    1   7 
    2    1   8 

我怎樣寫SELECT語句來獲得統計數據?

回答

0
select ofs.folder_group_id, ofs.folder_id, sum(Files.number_of_line_of_text) 
from ordered_file_stats ofs 
join Files on Files.id = ofs.file_id 
group by folder_group_id, folder_id 
相關問題