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 player.age AS age, player.name AS name \
  3. | ORDER BY $-.age ASC, $-.name DESC;
  4. +-------------+-----+---------------------+
  5. | VertexID | age | name |
  6. +-------------+-----+---------------------+
  7. | "player103" | 32 | "Rudy Gay" |
  8. +-------------+-----+---------------------+
  9. | "player102" | 33 | "LaMarcus Aldridge" |
  10. +-------------+-----+---------------------+
  11. | "player101" | 36 | "Tony Parker" |
  12. +-------------+-----+---------------------+
  13. | "player100" | 42 | "Tim Duncan" |
  14. +-------------+-----+---------------------+
  15. nebula> $var = GO FROM "player100" OVER follow \
  16. YIELD follow._dst AS dst; \
  17. ORDER BY $var.dst DESC;
  18. +-------------+
  19. | dst |
  20. +-------------+
  21. | "player125" |
  22. +-------------+
  23. | "player101" |
  24. +-------------+

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

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. +-----------------+----------+
  9. | "Manu Ginobili" | 41 |
  10. +-----------------+----------+
  11. | "Spurs" | __NULL__ |
  12. +-----------------+----------+
  13. nebula> MATCH (v:player{name:"Tim Duncan"}) --> (v2) \
  14. RETURN v2.name AS Name, v2.age AS Age \
  15. ORDER BY Age DESC;
  16. +-----------------+----------+
  17. | Name | Age |
  18. +-----------------+----------+
  19. | "Spurs" | __NULL__ |
  20. +-----------------+----------+
  21. | "Manu Ginobili" | 41 |
  22. +-----------------+----------+
  23. | "Tony Parker" | 36 |
  24. +-----------------+----------+

最后更新: September 9, 2021