我有一个DB,里面有他们参加的学生和课程。目标是找到所有具有相同姓氏的学生参加相同的课程并返回他们的firstNames和courseNames。 (s:Student)-[r:ENROLLEDIN]->(c:Course)
我尝试了很多东西,但我相信我还在想SQL。
如何考虑关系,在同一个类的节点上比较相同的属性值?我用Google搜索了,但没有找到答案。
尽量不要过多地减去我,我刚开始学习这个东西。 提前谢谢。
答案 0 :(得分:2)
下面是一个简单的查询,可以找到所有参加同一课程的姓氏相同的学生:
MATCH (s1:Student)-[:ENROLLEDIN]->(c1:Course),(s2:Student)-[:ENROLLEDIN]->(c2:Course)
WHERE s1.lastName = s2.lastName AND c1.name = c2.name
RETURN DISTINCT s1.firstName, c2.name ORDER BY s1.firstName;
答案 1 :(得分:0)
所以,我认为这个查询应该有效:
// match students and courses when students have the same name
MATCH (s1:Student)-[:ENROLLEDIN]->(c1:Course),
(s2:Student)-[:ENROLLEDIN]->(c2:Course)
WHERE s1.lastname = s2.lastname
// order by c1
WITH s1, s2, c1, c2 ORDER BY c1
// collect c1 as c1s, order by c2
WITH s1, s2, c2, collect(c1) AS c1s ORDER BY c2
// collect c2 as c2s. Pass to the next context when
// c1s = c2s. The array order matters.
WITH s1, s2, collect(c2) as c2s, c1s
WHERE c1s = c2s
RETURN s1.firstName, s2.firstName, c1s