I have a table with the following structure.
CREATE TABLE `score` (
`sid` INT NULL,
`name` INT NULL,
`rollno` INT NULL,
`examid` INT NULL,
`phone` INT NULL,
`dob` INT NULL,
`district` INT NULL,
`score` INT NULL,
`active` INT NULL
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
;
I need a query that will assign rank to a student based on their score and age. If two students have same mark, student with highest date of birth is given priority.
This is the sample data I have:
--------------------------------------------------------------------------------
sid | name | examid | phone | dob | district | score | date | active
--------------------------------------------------------------------------------
1 User1 1 12345 10/11/1983 209 10 10/11/2017 1
2 User2 1 34567 11/10/1983 209 10 10/11/2017 1
3 User3 1 34567 11/10/1985 209 20 10/11/2017 1
------------------------------------------------------------------------------
The result I am expecting is like this:
+----+------------------+----------------+
| id | name | rank |
+----+------------------+----------------+
| 2 | User2 | 1 |
| 3 | User3 | 2 |
| 2 | User1 | 3 |
My question is similar to this question (Get the rank of a user in a score table) but is there any other solution.
This is the current query I have which is working but how to display rank with the same query.
SELECT name, rollno, score
FROM score
WHERE examid = '1'
ORDER BY score DESC, dob ASC