Analysis of mysql sorting problems

Source: Internet
Author: User


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

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.