Question

Here is my query that works fine in either SqlServer or Oracle:

SELECT *
FROM (SELECT row_number() over (order by DateTime desc) as RN, Id, DateTime
      FROM MyDbTable
      WHERE Deleted = 'F' AND (Code >= 2) AND (Type LIKE '%stock%') 
     ) t
WHERE RN < 200 AND RN > 100;

Get the Id and Datetime from first 100 rows after the 100th row, ordered by DateTime desc, from MyDbTable where Deleted='F', Code >= 2 and Type contains the 'stock' string.

I want to make it run on MySql, but it doesn´t have the row_number() function. How can I accomplish the same results keeping the same structure using MySql ?

Thanks for help.

Was it helpful?

Solution

  SELECT Id, DateTime
  FROM MyDbTable
  WHERE Deleted = 'F' AND (Code >= 2) AND (Type LIKE '%stock%')
  order by DateTime desc
  limit 100, 100

and if you also need a increasing number:

  SELECT Id, DateTime, @rn := @rn + 1  as RN
  FROM MyDbTable
  cross join (select @rn := 0) r
  WHERE Deleted = 'F' AND (Code >= 2) AND (Type LIKE '%stock%')
  order by DateTime desc
  limit 100, 100
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top