Derby DB SQL, Select Rows starting from row number

ぐ巨炮叔叔 提交于 2020-01-12 14:53:26

问题


How can I select From X rows to Y rows in a SQL in derby?

For example:

  • I would like to select row 15 - 30, but not top 15.
  • Select all the row starting from row number 30.

I tried LIMIT and ROWNUM do not work, how can I do it in derby?


回答1:


According to the FAQ:

Derby does not support the LIMIT syntax. However, Derby 10.4 added the ROW_NUMBER function and Derby 10.7 added the OFFSET and FETCH clauses.

Derby also supports limiting the number of rows returned by a query through JDBC.
<...>

Starting with the 10.4.1.3 release Derby also supports limiting the number of rows using the ROW_NUMBER function.
<...>

The ROW_NUMBER function can also be used to select a limited number of rows starting with an offset, for example:
<...>

SELECT * FROM ( 
    SELECT ROW_NUMBER() OVER() AS rownum, myLargeTable.* 
    FROM myLargeTable 
) AS tmp 
WHERE rownum > 200000 AND rownum <= 200005; 

If you are using Derby 10.7 or newer you can, also, use the OFFSET and FETCH clauses:

SELECT * FROM T ORDER BY I 
    OFFSET 10 ROWS 
    FETCH NEXT 10 ROWS ONLY


来源:https://stackoverflow.com/questions/19606571/derby-db-sql-select-rows-starting-from-row-number

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!