An example of mysql sorting analysis I interviewed a company today and asked a question about how to retrieve the score ranking: for example, the table structure is as follows: namescorezhang50wang30li80wu20 requires that the ranking be output according to the score. I have mentioned this problem before. I want to write it out on paper. I said I had a computer and it came out... (I want to write SQL statements on paper.) So I wrote it on my computer in less than a minute after I went home ..., note:
SELECT @ I: = @ I + 1, name, score FROM 'tb', (select @ I: = 0) as I order by score desc; result output @ I: = @ I + 1 name score1 li 802 zhang 503 wang 304 wu 20 ========================== ============================================let others write programs on paper, I can only exert half of my skill. The interviewer needs to reflect on it... ==============================================================
Another solution to this question: set @ I: = 0; www.2cto.com select if (ISNULL (@ I), @ I: = 1, @ I: = @ I + 1 ), name, score from tb order by score desc;
Two SQL statements are required. The first one resets @ I to 0 (if it is not reset, @ I will use the result once at the next run), and the second one takes the rank again.
See the actual application...
Author spamer