在Neo4j中,Cypher是一种用于查询和操作图数据库的语言
- 并集(Union):
MATCH (a:Person) RETURN a UNION MATCH (b:Person) RETURN b
- 交集(Intersection):
MATCH (a:Person {name: "Alice"})-[:KNOWS]->(b:Person) RETURN a, b
- 差集(Difference):
MATCH (a:Person {name: "Alice"})-[:KNOWS]->(b:Person) WITH a, b MATCH (c:Person)-[:KNOWS]->(d:Person) WHERE d IN (a, b) RETURN c, d
- 子查询(Subquery):
MATCH (a:Person) WHERE ( SIZE([(a)<-[:KNOWS]-(b:Person) | b]) > 2 ) RETURN a
- 笛卡尔积(Cartesian Product):
MATCH (a:Person) MATCH (b:Person) RETURN a, b
- 限制结果集(Skip and Limit):
MATCH (a:Person)-[:KNOWS]->(b:Person) RETURN a, b SKIP 10 LIMIT 5
- 排序(Sort):
MATCH (a:Person)-[:KNOWS]->(b:Person) RETURN a, b ORDER BY b.age DESC
- 分组(Group By):
MATCH (a:Person)-[:KNOWS]->(b:Person) WITH a, COUNT(b) AS num_friends RETURN a, num_friends ORDER BY num_friends DESC
- 聚合函数(Aggregate Functions):
MATCH (a:Person)-[:KNOWS]->(b:Person) WITH a, COUNT(b) AS num_friends RETURN a, AVG(num_friends) AS avg_friends
这些示例展示了如何在Cypher中使用集合操作。您可以根据需要组合和调整这些操作以满足您的查询需求。