MySQL calculates a user-ranked SQL statement based on the answer score and time
Source: Internet
Author: User
MySQL calculates a user-ranked SQL statement based on the answer score and time
I now have such a table, I want to based on the activity ID, the maximum score, the minimum time, to find out the current user rankings.
Like what:
User id=6 user, I want to find out his rank rank=2 in the activity id=2 according to the total score (if the total score is the same, then according to the total time small calculation);
User id=1 The user, I want to find out his rank rank=3 in the activity id=1 according to the total score (if the total score is the same, then according to the total time small calculation).
Direct a SQL out rank.
This is my sql:
Select COUNT (*) rank from table name where score>= (select score from table name as B where user id=1) and times<= (select times from table name As C WHERE user id=1);
But when two users score the same time, it will be completely chaotic. Rank is going to be a mess. Seek the guidance of the great God, I am a novice!
------Solution--------------------
SELECT find_in_set (User id,group_concat (CAST (id as CHAR))
From (
SELECT *
From ' Rank '
WHERE Activity ID =1
ORDER by Score DESC, Time ASC
) Newrank
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.