Oracle兼容-语法-OFFSET … FETCH


1. 语法

  1. [ OFFSET offset { ROW | ROWS } ]
  2. [ FETCH { FIRST | NEXT } [ { row_count | percent PERCENT } ]
  3. { ROW | ROWS } { ONLY | WITH TIES } ]

2. 定义和用法

下面是关于 SELECT ... OFFSET ... FETCH 用法的说明:

  • NEXTFIRST 是具有相同意义的关键字。

  • ROWROWS 也是具有相同意义的关键字。

  • 关于 row_count | percent PERCENT

OFFSET 子句用于指定在行限制开始之前要跳过行数偏移量,OFFSET 子句是可选的。如果跳过它,则偏移量为0,行限制从第一行开始计算。

偏移量必须是一个数字或一个表达式(表达式计算结果值为一个数字)。偏移量遵守以下规则:

  1. 如果偏移量是负值,则将视为0

  2. 如果偏移量为NULL或大于查询返回的行数,则不返回任何行。

  3. 如果 row_count 包含一个小数,则小数部分被截断。

  4. PERCENT 百分值返回表数值的百分比的行数,如果 percent 计算后的结果包含小数,则向上补一位。例如:表数据共100条,percent=0.1 时计算结果为 100_0.1/100=0.1*,含有小数则向上补一位为 _1

  • 关于 ONLY | WITH TIES
  1. ONLY: 仅返回 FETCH NEXT/FIRST 后的行数或行数的百分比。

  2. WITH TIES:返回与 ORDER BY 最后一行相同值的记录 。注意:如果使用 WITH TIES,则必须在查询中指定一个 ORDER BY 子句。如果不这样做,查询将不会返回额外的行。

3. Oracle兼容说明

  1. 在Oracle中不可以和 FOR UPDATE 同时使用,但在GreatSQL中,允许 LIMIT OFFSETFOR UPDATE 同时使用。

  2. 如果查询列表中包含了相同的名称的字段,则需要用别名区分。

  3. 在GreatSQL中,OFFSET 偏移量存在隐式转换可能,不同函数的隐式转换结果可能存在不一致。

4. 示例

  1. greatsql> CREATE TABLE t1 (a INT);
  2. greatsql> INSERT INTO t1 VALUES (1), (1), (2), (3), (2);
  3. greatsql> SELECT * FROM t1
  4. OFFSET 2 ROWS;
  5. +------+
  6. | a |
  7. +------+
  8. | 2 |
  9. | 3 |
  10. | 2 |
  11. +------+
  12. 3 rows in set (0.00 sec)
  13. greatsql> SELECT * FROM t1
  14. FETCH FIRST ROW ONLY;
  15. +------+
  16. | a |
  17. +------+
  18. | 1 |
  19. +------+
  20. 1 row in set (0.00 sec)
  21. greatsql> SELECT * FROM t1
  22. FETCH FIRST ROWS ONLY;
  23. +------+
  24. | a |
  25. +------+
  26. | 1 |
  27. +------+
  28. 1 row in set (0.00 sec)
  29. greatsql> SELECT * FROM t1
  30. FETCH NEXT ROW ONLY;
  31. +------+
  32. | a |
  33. +------+
  34. | 1 |
  35. +------+
  36. 1 row in set (0.00 sec)
  37. greatsql> SELECT * FROM t1
  38. FETCH NEXT ROWS ONLY;
  39. +------+
  40. | a |
  41. +------+
  42. | 1 |
  43. +------+
  44. 1 row in set (0.00 sec)
  45. greatsql> SELECT * FROM t1 ORDER BY a
  46. OFFSET 2 ROW
  47. FETCH FIRST ROW ONLY;
  48. +------+
  49. | a |
  50. +------+
  51. | 2 |
  52. +------+
  53. 1 row in set (0.00 sec)
  54. greatsql> SELECT * FROM t1 ORDER BY a
  55. OFFSET 2 ROWS
  56. FETCH FIRST 1 ROW ONLY;
  57. +------+
  58. | a |
  59. +------+
  60. | 2 |
  61. +------+
  62. 1 row in set (0.00 sec)
  63. greatsql> SELECT * FROM t1
  64. FETCH FIRST ROW WITH TIES;
  65. +------+
  66. | a |
  67. +------+
  68. | 1 |
  69. +------+
  70. 1 row in set (0.00 sec)
  71. greatsql> SELECT * FROM t1 ORDER BY a
  72. FETCH FIRST ROW WITH TIES;
  73. +------+
  74. | a |
  75. +------+
  76. | 1 |
  77. | 1 |
  78. +------+
  79. 2 rows in set (0.00 sec)
  80. greatsql> SELECT * FROM t1 ORDER BY a
  81. OFFSET 2 ROWS
  82. FETCH FIRST ROW WITH TIES;
  83. +------+
  84. | a |
  85. +------+
  86. | 2 |
  87. | 2 |
  88. +------+
  89. 2 rows in set (0.00 sec)
  90. greatsql> SELECT * FROM t1
  91. FETCH FIRST 3 ROW ONLY;
  92. +------+
  93. | a |
  94. +------+
  95. | 1 |
  96. | 1 |
  97. | 2 |
  98. +------+
  99. 3 rows in set (0.00 sec)
  100. greatsql> SELECT * FROM t1
  101. FETCH NEXT 1+2 ROWS ONLY;
  102. +------+
  103. | a |
  104. +------+
  105. | 1 |
  106. | 1 |
  107. | 2 |
  108. +------+
  109. 3 rows in set (0.00 sec)
  110. greatsql> SELECT * FROM t1 ORDER BY a
  111. OFFSET 2 ROWS FETCH FIRST 3 ROW ONLY;
  112. +------+
  113. | a |
  114. +------+
  115. | 2 |
  116. | 2 |
  117. | 3 |
  118. +------+
  119. 3 rows in set (0.00 sec)
  120. greatsql> SELECT * FROM t1
  121. OFFSET 2 ROWS FETCH NEXT 3 ROW ONLY;
  122. +------+
  123. | a |
  124. +------+
  125. | 2 |
  126. | 3 |
  127. | 2 |
  128. +------+
  129. 3 rows in set (0.00 sec)
  130. greatsql> SELECT * FROM t1 ORDER BY a
  131. FETCH FIRST 3 ROWS WITH TIES;
  132. +------+
  133. | a |
  134. +------+
  135. | 1 |
  136. | 1 |
  137. | 2 |
  138. | 2 |
  139. +------+
  140. 4 rows in set (0.00 sec)
  141. greatsql> SELECT * FROM t1 ORDER BY a
  142. OFFSET 2 ROWS
  143. FETCH NEXT 3 ROWS WITH TIES;
  144. +------+
  145. | a |
  146. +------+
  147. | 2 |
  148. | 2 |
  149. | 3 |
  150. +------+
  151. 3 rows in set (0.00 sec)
  152. greatsql> SELECT * FROM t1
  153. FETCH NEXT 49 PERCENT ROWS ONLY;
  154. +------+
  155. | a |
  156. +------+
  157. | 1 |
  158. | 1 |
  159. | 2 |
  160. +------+
  161. 3 rows in set (0.00 sec)
  162. greatsql> SELECT * FROM t1
  163. FETCH NEXT 50 PERCENT ROWS ONLY;
  164. +------+
  165. | a |
  166. +------+
  167. | 1 |
  168. | 1 |
  169. | 2 |
  170. +------+
  171. 3 rows in set (0.00 sec)
  172. greatsql> SELECT * FROM t1
  173. OFFSET 2 ROWS
  174. FETCH NEXT 50 PERCENT ROWS ONLY;
  175. +------+
  176. | a |
  177. +------+
  178. | 2 |
  179. | 3 |
  180. | 2 |
  181. +------+
  182. 3 rows in set (0.00 sec)
  183. greatsql> SELECT * FROM t1
  184. FETCH NEXT 50 PERCENT ROWS WITH TIES;
  185. +------+
  186. | a |
  187. +------+
  188. | 1 |
  189. | 1 |
  190. | 2 |
  191. +------+
  192. 3 rows in set (0.00 sec)
  193. greatsql> SELECT * FROM t1 ORDER BY a
  194. OFFSET 1 ROWS
  195. FETCH NEXT 49 PERCENT ROWS WITH TIES;
  196. +------+
  197. | a |
  198. +------+
  199. | 1 |
  200. | 2 |
  201. | 2 |
  202. +------+
  203. 3 rows in set (0.00 sec)

问题反馈

联系我们

扫码关注微信公众号

greatsql-wx