来自查询的mysql sum值

时间:2011-06-02 14:53:56

标签: mysql

mysql查询如下:

SELECT count(f.id) as total, m1.fname FROM members m1 INNER JOIN new f ON m1.id = f.id

该查询的结果:

   total | fname  | 
  ------------------
    2    |  john
    3    |  mike
    5    |  july

如何使用此mysql查询对“total”列中的值进行求和?

2 个答案:

答案 0 :(得分:2)

在子查询上使用SUM

SELECT SUM(c.total) AS total_sum
FROM (
    SELECT count(f.id) as total, m1.fname
    FROM
        members m1 INNER JOIN
        new f ON m1.id = f.id
) AS c

答案 1 :(得分:0)

您应该使用sum()而不是count()。你查了一下吗?

SELECT sum(f.id) as total, m1.fname FROM members m1 INNER JOIN new f ON m1.id = f.id