Search code examples
mysqlsubqueryaggregation

SUM(subquery) in MYSQL


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?


Solution

  • Why don't you do this:

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