Mysql – How to select rows after a certain row using auto increment temporary columns

auto-incrementMySQLmysql-5.6

I have a table that has ID varchar(255) as PK. I have managed to create an auto increment temporary column rowNumber to use with a SELECT, as follows:

SELECT
   (@cnt := @cnt + 1) AS rowNumber, ID  
  from table
  CROSS JOIN (SELECT @cnt := 0) AS n 
where 
  (
    some conditions
    )
  )
  and date > {ts '2020-08-06 08:51:23.08'}
  ORDER BY ID
  LIMIT 10
;

Executing the above query gives me the following result:

rowNumber     ID
1           0000fb46-7eb1-48b4-8b47-6d99f636c92a
2           000198a5-0453-4339-b022-3050cad4e2b8
3           000ef1cc-1dcb-4162-806d-a040a29986d1
4           001de8e9-2809-4747-b12b-278b76a8bb5d
5           002080fb-21a8-437c-b468-243eb172cd1f
6           0024e99c-f576-404a-b14e-de8b4d6c6d73
7           004f2923-96ae-46ae-8d24-fd67cef40d16
8           006c8439-1ec0-486a-a372-7091d4a85eea
9           0078ce32-5858-4885-b207-c528561c2d40
10          0086618f-ab05-4c06-9c0a-7ec9fed1b82f

How can I modify the query above so that I can select the rows, after a certain rowNumber value, 4 for example. Could really use some help with this.

Best Answer

Use HAVING for checking rownames

SELECT
   (@cnt := @cnt + 1) AS `rowNumber`, ID  
  from table1
  CROSS JOIN (SELECT @cnt := 0) AS n 
where 
  (
    (1 = 1
    )
  )
  and date > '2020-08-06 08:51:23.08'
  HAVING `rowNumber` > 4
  ORDER BY ID  
  LIMIT 10
;