1

I have a table "student_points".

id    user_id     points   subject_id      
 1      10         45           22
 2      11         75           23
 3      12         78           24
 4      10         13           23
 5      12         65           23

and so on ...

This table contain about 1000 records of users and points. I want 10 records based on points (Max points first) So I can use mysql query as

Select * from student_points order by points limit 0,10

Now the requirement is that we need to group these 10 records based on user_id For example in first 10 records three are 3 students records so they should display in group. End result should be like

   id    user_id     points    subject_id  
    3      12         78           24
    5      12         65           23
    1      10         45           22
    4      10         13           23
    2      11         75           23

You can see that first record is based on most point and it student id is 12, now they are group according to user_id.

I tried two order by . I also tried to array_multisort after getting result but both are not working properly.

Please suggest any way, Either mysql query or group after getting result.

Thanks

6
  • Do you want 10 records for each user_id sorted by descending order of points? Commented Sep 21, 2016 at 10:47
  • 3
    ORDER BY user_id DESC, points DESC? Commented Sep 21, 2016 at 10:48
  • you want to sum up the points as well, right? Commented Sep 21, 2016 at 10:51
  • What would be final result you want ? Commented Sep 21, 2016 at 10:55
  • Hello , I edited question for better understanding , now there is subject_id. those 10 results there can be records of 3 students based on points in different subject , I just want to group them based on user_id . Commented Sep 21, 2016 at 10:59

3 Answers 3

1

To get your required result I have written the query :

SELECT * FROM (
    Select * from student_points order by points limit 0,10
) As st
GROUP BY user_id,subject_id
ORDER BY points DESC

Please try this. Let me know if this is not work for you.

Sign up to request clarification or add additional context in comments.

Comments

1

This should work just add a limit to whatever number you want to limit by

select sp.id, sp.user_id, sp.points 
from student_points sp
join (select user_id, max(points) as sort_by from student_points group by user_id) sort_table on sp.user_id = sort_table.user_id
order by sort_table.sort_by desc, sp.user_id, sp.points desc;

Comments

0

I guess you need to wrap up your query in a subquery and later sort the results based on user_id and points.

SELECT * FROM 
(
  Select * from student_points order by points limit 0,10
) AS t
ORDER BY 
  t.user_id DESC, 
  t.points DESC

7 Comments

I would love to hear the reason behind downvoting
@1000111 , Hello please read question again, I have edited .
Your requirement can still be achieved by this query. Please give this a try and let me know @Rich5757
This will not show the student ID / group with the highest score first, so the conditions are not met completely.
Maybe I am missing something. Actually I didn't get your message. Would you please post an answer here @jeroen
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.