如何在cypher中编写半连接查询?

时间:2016-08-20 08:10:12

标签: neo4j cypher graph-databases

我想在cypher中编写半连接查询。例如,我想找教授,教授在下图中至少有一名成绩超过9.0的学生:Sample Graph

因此样本图的输出应该如下:

P2
P3

这将通过以下方式在SQL中完成:

Select Professor.name
From Professor
where exists (
                select * from Student
                where Student.grade >= 9.0 and
                Student.guide_id = Professor.id
             )

1 个答案:

答案 0 :(得分:1)

例如,假设我们按如下方式向教授和学生添加数据:

UNWIND RANGE(1,1000) as i
 WITH 'professor_' + toInt(rand()*20) as pname, 
      'student_' + i as sname
 MERGE (P:Professor:Person {name: pname})
 MERGE (S:Student:Person   {name: sname}) SET S.grade = rand()*10
 MERGE (P)-[:guide]->(S)

查询学生成绩≥> 9.0的教授名单:

MATCH (P:Professor)-[:guide]->(S:Student) WHERE S.grade >= 9.0
RETURN distinct P