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
精彩评论