Agregate密码查询

时间:2018-06-19 10:41:48

标签: neo4j cypher

这是我在Neo4j中的数据库:

CREATE (Alex:Person {name:'Alex', phone:'0420965111'})
CREATE (Oxana:Person {name:'Oxana', email:'oxana@mail.com'})
CREATE (Tango:Dance {name:'Tango'})
CREATE (Ballet:Dance {name:'Ballet'})
CREATE (Zouk:Dance {name:'Zouk'})
CREATE (Saturday:Day {name:'Saturday'})
CREATE (Sunday:Day {name:'Sunday'})
CREATE (Wednesday:Day {name:'Wednesday'})
MERGE (Alex)-[:LIKES]->(Tango)
MERGE (Alex)-[:LIKES]->(Zouk)
MERGE (Oxana)-[:LIKES]->(Tango)
MERGE (Oxana)-[:LIKES]->(Ballet)
MERGE (Alex)-[:AVAILABLE_ON]->(Sunday)
MERGE (Alex)-[:AVAILABLE_ON]->(Wednesday)
MERGE (Oxana)-[:AVAILABLE_ON]->(Sunday)
MERGE (Oxana)-[:AVAILABLE_ON]->(Saturday)

enter image description here

我需要一个超过1个人的名单,他们喜欢同一个舞蹈并且在同一天可用。如何写一个返回这个的查询?: "Sunday", "Tango", ["Alex","Oxana"]

这几乎有效:match (p:Person), (d:Dance), (day:Day) where (p)-[:LIKES]->(d) and (p)-[:AVAILABLE_ON]->(day) return day.name, d.name, collect(p.name), count(*)但我不知道如何排除count(*)小于2的记录。

1 个答案:

答案 0 :(得分:1)

您可以使用WITH

match (p:Person), (d:Dance), (day:Day)
where (p)-[:LIKES]->(d) and (p)-[:AVAILABLE_ON]->(day)
with day.name as day, d.name as dance, collect(p.name) as names, count(*) as count
where count >= 2
return day, dance, names

来自docs

  

WITH子句允许将查询部分链接在一起,用管道将   来自一个的结果用作起点或准则   接下来。

此外,您可以添加约束(WHERE子句)以过滤数据。