Número de fila en mySQL

Is it possible to get the row number in MySQL? Say I have a 'table'

ID  tag name
1   A   alpha
4   B   beta
5   C   gamma
8   D   ceta

How can I get in MySQL that, for example, 'C' is the 3rd row in that table? Following:

SET @pos=0;
SELECT @pos:=@pos+1,tag FROM table ORDER BY tag ASC;

counts the rows as it should. But (sorry for ignorant code)

SET @pos=0;
SELECT @pos:=@pos+1,tag FROM table where tag='C' ORDER BY tag ASC;

gives 1 row as a result, with pos as 0, as it probably should.

Is there a way to get the 'pos' to be '3' as I need it to be? (Ordering would be important as well, whether it is relevant to the question or not..)

preguntado el 05 de mayo de 13 a las 19:05

How to know a row number of a specific row in MySql?. An unanswered question but it contains an SQL query to get a row number of a specific row. -

Tables don't have row numbers because they don't actually have ordering. In the real world, of course, rows are put on the harddrive in some order, but it's not supposed to mean anything. If you want ordered numbers, they'll need to either be in a column for that purpose, or subject to the particulars of your ORDER BY clause. -

I'm probably just misunderstanding the question but what would be wrong with adding LIMIT 1 OFFSET 3 to your existing SELECT? Incidentally, the "row number" in this case is an artifact of your ORDER BY clause. You seem to be asking for one item offset from the beginning of your query results by three (or perhaps by two, I'm not sure). -

I'm probably misunderstanding your comment (not too good in MySQL am I, as you could tell) but the problem is that I don't know that 'C' is number 3.. What I need to know is, what row is it.. -

@Konservin: it's not any row. There's no row number, period, unless you create a column that stores some sequential numbers. -

3 Respuestas

Puede usar este

Select rownum from (
SELECT @rownum:=@rownum+1 rownum, t.*FROM (SELECT @rownum:=0) r, table t order by tag) as t1
where tag = 'C'

contestado el 05 de mayo de 13 a las 19:05

Thank You Dany, this actually seems to work perfectamente judgung by some quick testing. 'C' is 3 every time now. What keeps me from marking it as an answer is Siride's comment.. Can there be some problem there? - Konservín

@Konservin: there's nothing wrong here, per se. This is a valid way to create a dynamic number of rows for a particular resultset. As long as you are okay with that, then this is a fine answer (or my suggestion to add a column with a persisted ordering). Note, however, that if you insert any rows between, say, B and C, the numbering will change with this query. - sirir

@Konservin to explain further, @siride is right there are no row numbers for queries, but what is happening here, is that manually we're adding a column that will contain a row number, and we're incrementing it for each row @rownum:=@rownum+1 and calling this column rownum. Then we are selecting from this table that contains the row number (which we made) - dany y

In case your IDs are strictly increasing with row numbers, you can do

SELECT COUNT(*) FROM tbl WHERE ID <= (SELECT ID FROM tbl WHERE tag = 'C');

I am not sure what you mean by the ordering though.

Side note: Your code

SET @pos=0;
SELECT @pos:=@pos+1,tag FROM tbl where tag='C' ORDER BY tag ASC;

cant work, because here @pos operates on the result set only, which consists of only one record.

contestado el 05 de mayo de 13 a las 19:05

Funnily enough, it works.. :) It returns one record with pos=0 - Konservín

i meant not that your code does nothing at all. I meant it does not come back with the number 3 that you want to get. But Is see that you are happy with Dany Y answer. So i guess your problem is solved. - Luksch

Actually, there's one possibility that I didn't consider before:

SELECT count(1) FROM table WHERE tag <= 'C' ORDER BY tag

This seems to do the same thing, and a bit faster, too.. Or am I missing something?

contestado el 05 de mayo de 13 a las 21:05

that is the same solution as mine, just that you now rely on the ordering of tag. I thought you want to keep the original order by id. - Luksch

Yes, I see it now.. Sorry for not looking into your answer more deeply. +many :) - Konservín

No problem. As I said, I am happy that your problem seems to be solved. - Luksch

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.