簡體   English   中英

MySQL查詢匯總表中的一列,其中兩個列組合在一起時是唯一的

[英]Mysql query to sum a column from a table where another two columns are unique when combined together

我有一個Mysql表,其中結合了三個表。 該表為三個外鍵的每個組合存儲一些統計信息。

例如:

1) 文件表

列-

id - primary key
name
number_of_lines_of_text - the stats field

2) 文件夾表與文件表具有一對多關系

列-

id - primary key
name

3) Folder_Groups表folders表具有多對多關系

列-

id - primary key
name

最后組合表ordered_file_stats

列-

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

我需要為ordered_file_stats表中的folder_group_idfolder_id的每個唯一組合選擇“ 文件”表的number_of_lines_of_text字段並對其求和。

可能看起來像

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 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM