tags:

views:

48

answers:

3

I was wondering how can I combine these two queries.

Here is the 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
+1  A: 

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
knittl
I would go with this one if the table structure reflects this query, even though it is not clear from the question.
DrColossos
@DrColossos: yes, table structure would be quite some help
knittl
+2  A: 

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.

Thariama
+1  A: 

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
Balint Pato
"assuming grades table has a user_id column" - this seems to be not the case here
Thariama
you've got a point, but if this is not the case then i totally do not understand why he wants to combine the queries :)
Balint Pato