How Do I include the current row in a mysql select statement

Posted on 2006-04-26
Last Modified: 2008-03-03
Hi Experts--

The question title says it all. basically i have a list of scores, i want to select them in order and add a rank column to the result. something like this:

SELECT score, CURRENT_ROW as 'rank'
FROM tbl_scores
ORDER BY score

i don't think CURRENT_ROW is actually a variable in MySQL. I need to know what to use in it's place



Question by:jacobbdrew
    LVL 4

    Assisted Solution

    Try this friend. Or you can write something similar to this in your MySQL

    SELECT score, (SELECT COUNT(*) FROM tbl_scores B WHERE B.score <= A.score) as 'rank'
    FROM tbl_scores A
    ORDER BY score
    Sincerely yours
    LVL 14

    Accepted Solution

    SET @rank=0;
    SELECT score, (@rank:=@rank+1) as 'rank'
    FROM tbl_scores
    ORDER BY score;

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Introduction After workin in a plethora of programming languages like C, Pro*C, ESQL/C, C++, VC++, VB, Java, HTML,JavaScipt etc, technologies and frameworks like JSP, Servlets, Struts, Spring, IBatis etc and databases like MS Access, SQLServer, Inf…
    SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now