0

The mysql query looks like:

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

The result of that query:

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

How can I sum values from column "total" using this mysql query?

3
  • Just curious, but why are you summing ID's? Commented Jun 2, 2011 at 14:58
  • I want to sum values from column "total", not the values of ID's Commented Jun 2, 2011 at 15:00
  • Just an observation : using ids for semantic meaning of your model it could not be the best idea to model things. Id should be only used for persistence of models Commented Jun 9, 2011 at 15:33

2 Answers 2

2

Use SUM on the subquery:

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
Sign up to request clarification or add additional context in comments.

Comments

0

You should use sum() instead of count(). did you check sum?

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

2 Comments

in that case I get sum od "id" values, not values from "total" column
sorry I understand you want to sum ids.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.