简体   繁体   English

MySQL-将值求和到唯一ID

[英]MySQL - sum values to unique ids

I have a table with file information, and I query -- SELECT DISTINCT File, Numbers FROM Table -- the table to create a two column table with the name of the file and some numbers ie 我有一个包含文件信息的表,然后查询-SELECT DISTINCT文件,Numbers FROM表-用该表创建一个两列表,其中包含文件名和一些数字,即

    File |  Numbers
    ---------------
    A    |    1
    A    |    2
    A    |    4
    B    |    3
    B    |    1
    B    |    2
    C    |    5
    C    |    3
    C    |    1

I am trying get this result, to sum up this query with the unique file name ie 我正在尝试获得此结果,以唯一的文件名称总结此查询即

    File |  Numbers
    ---------------
    A    |    7
    B    |    6
    C    |    9

I can get the individual sum according to the file name via SELECT File, SUM(Numbers) FROM (SELECT DISTINCT File, Numbers FROM Table) WHERE File = 'A' 我可以通过SELECT File,SUM(Numbers)FROM(SELECT DISTINCT File,Numbers FROM Table)WHERE File ='A'根据文件名获取单个总和

but I want to have all three present in my results. 但我希望所有三个结果都在我的结果中。 I have tried - SELECT File, SUM(Numbers) FROM (SELECT DISTINCT File, Numbers FROM Table) but get the result of 我试过了-SELECT文件,SUM(Numbers)从(SELECT DISTINCT文件,Numbers从表),但得到的结果

    File |  Numbers
    ---------------
    C    |    22

您需要一个group by

select `File`, sum(`Numbers`) from `tbl` group  by `File`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM