Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get row position in MySQL?

First of all, I've tried to google this, but only came up with this kind of answer: MySQL - Get row number on select

What I need is, to "get row position by speciefic id".

With the example from above link, I tried to add the "WHERE CLAUSE", but, the result of "rank" was always 1.

So, if my table has auto increment id field, and is not in specific order (for example, after id 1 comes 3, not 2, because id 2 was deleted from database), how can I get the position of for example id 4?

Is there any possible way to get, what I want?

like image 800
Lado Lomidze Avatar asked Dec 26 '22 03:12

Lado Lomidze


1 Answers

Assuming that, by "position", you mean when ordered by id:

SELECT COUNT(*) FROM my_table WHERE id <= 4
like image 148
eggyal Avatar answered Dec 28 '22 20:12

eggyal