问题描述:

I want to join 2 tables A and B, and on the result i want to group duplicate column and sum other column. and i used with :

SELECT VALUE, SUM(ARATING) FROM (SELECT A.QID, A.VALUE, B.ARating

FROM A JOIN B

ON A.QID = B.QID

AND B.Nickname='someuser')

GROUP BY VALUE;

And I get an error for some reason.

网友答案:

In MySQL, you need a table alias on the subquery:

SELECT VALUE, SUM(ARATING)
FROM (SELECT A.QID, A.VALUE, B.ARating  
      FROM A JOIN
           B
           ON A.QID = B.QID AND B.Nickname = 'someuser'
     ) ab
GROUP BY VALUE;

However, you don't need the subquery at all:

SELECT A.VALUE, SUM(B.ARATING)
FROM A JOIN
     B
     ON A.QID = B.QID AND B.Nickname = 'someuser'
GROUP BY A.VALUE;
相关阅读:
Top