ORDER BY

ORDER BY子句指定输出结果的排序规则。

  • 在原生nGQL中,必须在YIELD子句之后使用管道符(|)和ORDER BY子句。

  • 在openCypher方式中,不允许使用管道符。在RETURN子句之后使用ORDER BY子句。

排序规则分为如下两种:

  • ASC(默认): 升序。
  • DESC: 降序。

原生nGQL语法

  1. <YIELD clause>
  2. ORDER BY <expression> [ASC | DESC] [, <expression> [ASC | DESC] ...];

Compatibility

原生nGQL语法中,ORDER BY命令后必须使用引用符$-.。但在2.5.0之前的版本中不需要。

示例

  1. nebula> FETCH PROP ON player "player100", "player101", "player102", "player103" \
  2. YIELD properties(vertex).age AS age, properties(vertex).name AS name \
  3. | ORDER BY $-.age ASC, $-.name DESC;
  4. +-------------+-----+---------------------+
  5. | VertexID | age | name |
  6. +-------------+-----+---------------------+
  7. | "player103" | 32 | "Rudy Gay" |
  8. | "player102" | 33 | "LaMarcus Aldridge" |
  9. | "player101" | 36 | "Tony Parker" |
  10. | "player100" | 42 | "Tim Duncan" |
  11. +-------------+-----+---------------------+
  12. nebula> $var = GO FROM "player100" OVER follow \
  13. YIELD dst(edge) AS dst; \
  14. ORDER BY $var.dst DESC;
  15. +-------------+
  16. | dst |
  17. +-------------+
  18. | "player125" |
  19. | "player101" |
  20. +-------------+

OpenCypher方式语法

  1. <RETURN clause>
  2. ORDER BY <expression> [ASC | DESC] [, <expression> [ASC | DESC] ...];

示例

  1. nebula> MATCH (v:player) RETURN v.name AS Name, v.age AS Age \
  2. ORDER BY Name DESC;
  3. +-----------------+-----+
  4. | Name | Age |
  5. +-----------------+-----+
  6. | "Yao Ming" | 38 |
  7. | "Vince Carter" | 42 |
  8. | "Tracy McGrady" | 39 |
  9. | "Tony Parker" | 36 |
  10. | "Tim Duncan" | 42 |
  11. +-----------------+-----+
  12. ...
  13. # 首先以年龄排序,如果年龄相同,再以姓名排序。
  14. nebula> MATCH (v:player) RETURN v.age AS Age, v.name AS Name \
  15. ORDER BY Age DESC, Name ASC;
  16. +-----+-------------------+
  17. | Age | Name |
  18. +-----+-------------------+
  19. | 47 | "Shaquille O'Neal" |
  20. | 46 | "Grant Hill" |
  21. | 45 | "Jason Kidd" |
  22. | 45 | "Steve Nash" |
  23. +-----+-------------------+
  24. ...

NULL值的排序

升序排列时,会在输出的最后列出NULL值,降序排列时,会在输出的开头列出NULL值。

  1. nebula> MATCH (v:player{name:"Tim Duncan"}) --> (v2) \
  2. RETURN v2.name AS Name, v2.age AS Age \
  3. ORDER BY Age;
  4. +-----------------+--------------+
  5. | Name | Age |
  6. +-----------------+--------------+
  7. | "Tony Parker" | 36 |
  8. | "Manu Ginobili" | 41 |
  9. | "Spurs" | UNKNOWN_PROP |
  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;
  14. +-----------------+--------------+
  15. | Name | Age |
  16. +-----------------+--------------+
  17. | "Spurs" | UNKNOWN_PROP |
  18. | "Manu Ginobili" | 41 |
  19. | "Tony Parker" | 36 |
  20. +-----------------+--------------+

最后更新: November 1, 2021