SUM(subquery) in MYSQL

Dänu picture Dänu · Jan 6, 2012 · Viewed 52.5k times · Source

Basically, I am trying the following:

SELECT m.col1, SUM(SELECT col5 FROM table WHERE col2 = m.col1)
FROM table AS m

This doesn't seem to work. Is there any solution?

Answer

Mithrandir picture Mithrandir · Jan 6, 2012

Why don't you do this:

SELECT m.col1, (SELECT SUM(col5) FROM table WHERE col2 = m.col1)
FROM table AS m