Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With MySQL, how can I generate a column containing the record index in a table?

Tags:

indexing

mysql

Is there any way I can get the actual row number from a query?

I want to be able to order a table called league_girl by a field called score; and return the username and the actual row position of that username.

I'm wanting to rank the users so i can tell where a particular user is, ie. Joe is position 100 out of 200, i.e.

User Score Row Joe  100    1 Bob  50     2 Bill 10     3 

I've seen a few solutions on here but I've tried most of them and none of them actually return the row number.

I have tried this:

SELECT position, username, score FROM (SELECT @row := @row + 1 AS position, username, score         FROM league_girl GROUP BY username ORDER BY score DESC)  

As derived

...but it doesn't seem to return the row position.

Any ideas?

like image 324
TheBounder Avatar asked Jun 27 '10 09:06

TheBounder


People also ask

How do I create an index column in MySQL?

Generally, we create an index at the time of table creation in the database. The following statement creates a table with an index that contains two columns col2 and col3. If we want to add index in table, we will use the CREATE INDEX statement as follows: mysql> CREATE INDEX [index_name] ON [table_name] (column names)

How can I get column details of a table in MySQL?

You can get the MySQL table columns data type with the help of “information_schema. columns”. SELECT DATA_TYPE from INFORMATION_SCHEMA. COLUMNS where table_schema = 'yourDatabaseName' and table_name = 'yourTableName'.

Can I CREATE INDEX on all columns of a table?

No, you should not index all of your columns, and there's several reasons for this: There is a cost to maintain each index during an insert, update or delete statement, that will cause each of those transactions to take longer. It will increase the storage required since each index takes up space on disk.

What is index record in MySQL?

An index record, is a row of an index table. Every time you create a table, there will be another datastructure representing the index. In InnoDB the index is stored in a tree (B+Tree).


1 Answers

You may want to try the following:

SELECT  l.position,          l.username,          l.score,         @curRow := @curRow + 1 AS row_number FROM    league_girl l JOIN    (SELECT @curRow := 0) r; 

The JOIN (SELECT @curRow := 0) part allows the variable initialization without requiring a separate SET command.

Test case:

CREATE TABLE league_girl (position int, username varchar(10), score int); INSERT INTO league_girl VALUES (1, 'a', 10); INSERT INTO league_girl VALUES (2, 'b', 25); INSERT INTO league_girl VALUES (3, 'c', 75); INSERT INTO league_girl VALUES (4, 'd', 25); INSERT INTO league_girl VALUES (5, 'e', 55); INSERT INTO league_girl VALUES (6, 'f', 80); INSERT INTO league_girl VALUES (7, 'g', 15); 

Test query:

SELECT  l.position,          l.username,          l.score,         @curRow := @curRow + 1 AS row_number FROM    league_girl l JOIN    (SELECT @curRow := 0) r WHERE   l.score > 50; 

Result:

+----------+----------+-------+------------+ | position | username | score | row_number | +----------+----------+-------+------------+ |        3 | c        |    75 |          1 | |        5 | e        |    55 |          2 | |        6 | f        |    80 |          3 | +----------+----------+-------+------------+ 3 rows in set (0.00 sec) 
like image 99
Daniel Vassallo Avatar answered Oct 21 '22 11:10

Daniel Vassallo