I have a Mysql table where it combines three tables. This table stores some statistics for each combination of the three foreign keys.
eg:
1)A Files table
columns-
id - primary key
name
number_of_lines_of_text - the stats field
2)A Folders table has a one to many relationship with files table
columns-
id - primary key
name
3)A Folder_Groups table has a many to many relationship with the folders table
columns-
id - primary key
name
Finally the c开发者_如何学Combination table 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
I need to select and sum the number_of_lines_of_text field of the Files table for each unique combination of the folder_group_id and the folder_id in the ordered_file_stats table.
Which may look like
folder_group_id folder_id file_id
1 2 1
1 2 2
1 3 9
2 1 7
2 1 8
How do I write the select statement to obtain the stats?
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
精彩评论