Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL get row position in ORDER BY

With the following MySQL table:

+-----------------------------+ + id INT UNSIGNED             + + name VARCHAR(100)           + +-----------------------------+ 

How can I select a single row AND its position amongst the other rows in the table, when sorted by name ASC. So if the table data looks like this, when sorted by name:

+-----------------------------+ + id | name                   + +-----------------------------+ +  5 | Alpha                  + +  7 | Beta                   + +  3 | Delta                  + + .....                       + +  1 | Zed                    + +-----------------------------+ 

How could I select the Beta row getting the current position of that row? The result set I'm looking for would be something like this:

+-----------------------------+ + id | position | name        + +-----------------------------+ +  7 |        2 | Beta        + +-----------------------------+ 

I can do a simple SELECT * FROM tbl ORDER BY name ASC then enumerate the rows in PHP, but it seems wasteful to load a potentially large resultset just for a single row.

like image 822
leepowers Avatar asked Sep 01 '10 02:09

leepowers


People also ask

How do I sort a row in MySQL?

The MySQL ORDER BY Keyword The ORDER BY keyword is used to sort the result-set in ascending or descending order. The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.

What is @rowindex in MySQL?

MySQL ROW_NUMBER() Function. The ROW_NUMBER() function in MySQL is used to returns the sequential number for each row within its partition. It is a kind of window function. The row number starts from 1 to the number of rows present in the partition.

How do I change the position of a row in SQL?

if (newPosition > oldPosition) { UPDATE people SET position = position - 1 WHERE listId = 1 AND position <= @newPosition AND Name != "Frank"; UPDATE people SET position = @newPos WHERE listId = 1 AND Name="Frank"; } else { … }

How do I sort data in MySQL?

Use the ORDER BY clause to sort the result set by one or more columns. Use the ASC option to sort the result set in ascending order and the DESC option to sort the result set in descending order. The ORDER BY clause is evaluated after the FROM and SELECT clauses.


1 Answers

Use this:

SELECT x.id,         x.position,        x.name   FROM (SELECT t.id,                t.name,                @rownum := @rownum + 1 AS position           FROM TABLE t           JOIN (SELECT @rownum := 0) r       ORDER BY t.name) x  WHERE x.name = 'Beta' 

...to get a unique position value. This:

SELECT t.id,        (SELECT COUNT(*)           FROM TABLE x          WHERE x.name <= t.name) AS position,        t.name       FROM TABLE t        WHERE t.name = 'Beta' 

...will give ties the same value. IE: If there are two values at second place, they'll both have a position of 2 when the first query will give a position of 2 to one of them, and 3 to the other...

like image 144
OMG Ponies Avatar answered Oct 01 '22 20:10

OMG Ponies