Mysql查询合计一个表中的列,其中另一个两列合并在一起时是唯一的

问题描述:

我有一个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语句来获得统计数据?

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