0

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.

9
  • Your version of MySQL is critical for answering this!
    – Vérace
    Commented Aug 6, 2020 at 11:12
  • @Vérace I'm trying to get this done for MySQL 5.6 Commented Aug 6, 2020 at 11:15
  • Since there's no sample data, I can't test anything, but can you not wrap the above query in a SELECT and add a WHERE rowNumber > 4? Or does the temporary variable preclude that?
    – Vérace
    Commented Aug 6, 2020 at 11:29
  • @Vérace tried doing that, didn't work. Commented Aug 6, 2020 at 11:43
  • I suppose an upgrade is out of the question? MySQL has taken large steps recently!
    – Vérace
    Commented Aug 6, 2020 at 11:44

1 Answer 1

1

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
;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.