如何组合2个MySQL查询?

时间:2010-06-21 07:44:39

标签: mysql

我想知道如何将这两个查询结合起来。

这是MySQL查询。

 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

3 个答案:

答案 0 :(得分:2)

不,你不能,因为你有两个完全分开的查询 - 因此你将无法使用 JOIN 来组合这些。

为此,他们需要至少有一个共同的领域。

答案 1 :(得分:1)

我假设你想要总结每位用户的积分:

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

答案 2 :(得分:1)

我会尝试这个(假设成绩表有一个user_id列)

 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