开发者

How to combine 2 MySQL queries?

I was wondering how can I combine these two queries.

Here is t开发者_C百科he MySQL queries.

 SELECT COUNT(user_id) as users, user_id
 FROM users_text
 GROUP BY user_id


 SELECT SUM(grade_points) as 'points'
 FROM grades
 ORDER BY points DESC


No, you cannot, cause you have two totally separated queries - therefor you won't be able to combine these using JOIN.

For that they need to have at least one common field.


i assume here you want to sum up the points per user:

  SELECT user_id, SUM(grade_points) as 'points'
    FROM grades
GROUP BY user_id
ORDER BY points DESC


I would try this (assuming grades table has a user_id column)

 SELECT COUNT(user_id) as users, user_id, (SELECT SUM(g.grade_points)  FROM grades g WHERE g.user_id = user_id ) as 'points'
 FROM users_text
 GROUP BY user_id
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜