LIMIT

LIMIT子句限制输出结果的行数。

  • 在nGQL扩展中,必须使用管道符(|),可以忽略偏移量。

  • 在openCypher方式中,不允许使用管道符,可以使用SKIP指明偏移量。

nGQL扩展语法

在nGQL扩展中,LIMIT的工作原理与SQL相同,必须和管道符一起使用。LIMIT子句接收一个或两个参数。参数的值必须是非负整数。

  1. YIELD <var>
  2. [| LIMIT [<offset_value>,] <number_rows>];
参数说明
var排序的列或计算结果。
offset_value偏移量,即定义从哪一行开始返回。索引从0开始。默认值为0,表示从第一行开始返回。
number_rows返回的总行数。

示例

  1. # 从排序结果中返回第2行开始的3行数据。
  2. nebula> GO FROM "player100" OVER follow REVERSELY \
  3. YIELD $$.player.name AS Friend, $$.player.age AS Age \
  4. | ORDER BY Age,Friend \
  5. | LIMIT 1, 3;
  6. +-------------------+-----+
  7. | Friend | Age |
  8. +-------------------+-----+
  9. | "Danny Green" | 31 |
  10. +-------------------+-----+
  11. | "Aron Baynes" | 32 |
  12. +-------------------+-----+
  13. | "Marco Belinelli" | 32 |
  14. +-------------------+-----+

openCypher方式语法

  1. RETURN <var>
  2. [SKIP <offset>]
  3. [LIMIT <number_rows>];
参数说明
var排序的列或计算结果。
offset偏移量,即定义从哪一行开始返回。索引从0开始。默认值为0,表示从第一行开始返回。
number_rows返回的总行数量。

offsetnumber_rows可以使用表达式,但是表达式的结果必须是非负整数。

示例

  1. nebula> MATCH (v:player) RETURN v.name AS Name, v.age AS Age \
  2. ORDER BY Age LIMIT 5;
  3. +-------------------------+-----+
  4. | Name | Age |
  5. +-------------------------+-----+
  6. | "Luka Doncic" | 20 |
  7. +-------------------------+-----+
  8. | "Ben Simmons" | 22 |
  9. +-------------------------+-----+
  10. | "Kristaps Porzingis" | 23 |
  11. +-------------------------+-----+
  12. | "Giannis Antetokounmpo" | 24 |
  13. +-------------------------+-----+
  14. | "Kyle Anderson" | 25 |
  15. +-------------------------+-----+
  16. nebula> MATCH (v:player) RETURN v.name AS Name, v.age AS Age \
  17. ORDER BY Age LIMIT rand32(5);
  18. +-------------------------+-----+
  19. | Name | Age |
  20. +-------------------------+-----+
  21. | "Luka Doncic" | 20 |
  22. +-------------------------+-----+
  23. | "Ben Simmons" | 22 |
  24. +-------------------------+-----+
  25. | "Kristaps Porzingis" | 23 |
  26. +-------------------------+-----+
  27. | "Giannis Antetokounmpo" | 24 |
  28. +-------------------------+-----+

SKIP示例

用户可以单独使用SKIP <offset>设置偏移量,后面不需要添加LIMIT <number_rows>

  1. nebula> MATCH (v:player{name:"Tim Duncan"}) --> (v2) \
  2. RETURN v2.name AS Name, v2.age AS Age \
  3. ORDER BY Age DESC SKIP 1;
  4. +-----------------+-----+
  5. | Name | Age |
  6. +-----------------+-----+
  7. | "Manu Ginobili" | 41 |
  8. +-----------------+-----+
  9. | "Tony Parker" | 36 |
  10. +-----------------+-----+
  11. nebula> MATCH (v:player{name:"Tim Duncan"}) --> (v2) \
  12. RETURN v2.name AS Name, v2.age AS Age \
  13. ORDER BY Age DESC SKIP 1+1;
  14. +---------------+-----+
  15. | Name | Age |
  16. +---------------+-----+
  17. | "Tony Parker" | 36 |
  18. +---------------+-----+

用户也可以同时使用SKIP <offset>LIMIT <number_rows>,返回中间的部分数据。

  1. nebula> MATCH (v:player{name:"Tim Duncan"}) --> (v2) \
  2. RETURN v2.name AS Name, v2.age AS Age \
  3. ORDER BY Age DESC SKIP 1 LIMIT 1;
  4. +-----------------+-----+
  5. | Name | Age |
  6. +-----------------+-----+
  7. | "Manu Ginobili" | 41 |
  8. +-----------------+-----+