MySQL row number

Let's say I have a MySQL query, for example:

SELECT id, name, surname FROM employees ORDER BY id

The result is:

id    name    surname
 1     Peter   Smith
 2     John    Banjo
...
 1384  Will    Levenstein

While this is an ordered query, I can always assume (until I change the table) that it John Banjowill come out second.

Now, if my request was

SELECT id, name, surname FROM employees WHERE name = 'John' AND surname = 'Banjo'

Can I somehow get the number of rows in the first query? I am trying to do this in a much more complex, but always ordered request, is there any way to archive this?

+3
source share
1 answer
 SELECT x.id, x.name, x.surname, x.rownum
 FROM (
      SELECT @rownum:=@rownum+1 rownum, t.*
      FROM (SELECT @rownum:=0) r, employees t
      ORDER BY Id
 ) x
 WHERE x.name = 'John' 
 AND x.surname = 'Banjo'
+2
source

Source: https://habr.com/ru/post/1761741/


All Articles