문제

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.

도움이 되었습니까?

해결책

  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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top