Neo4J 试图找到具有匹配 属性 值的所有节点
Neo4J Trying to find all nodes with matching property values
我有一个数据库,里面有学生和他们正在参加的课程。 objective 是查找所有具有相同姓氏且参加相同课程的学生以及 return 他们的名字和课程名称。 (s:Student)-[r:ENROLLEDIN]->(c:Course)
我脑子里想了很多东西,但我相信我还在想 SQL。
如何在考虑关系的情况下比较相同 class 节点的相同 属性 值?我已经用谷歌搜索了,但没有找到答案。
尽量不要贬低我太多,我才刚开始学这东西。
提前致谢。
所以,我认为这个查询应该有效:
// 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
下面是一个简单的查询,用于查找参加同一课程的所有具有相同姓氏的学生:
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;
我有一个数据库,里面有学生和他们正在参加的课程。 objective 是查找所有具有相同姓氏且参加相同课程的学生以及 return 他们的名字和课程名称。 (s:Student)-[r:ENROLLEDIN]->(c:Course)
我脑子里想了很多东西,但我相信我还在想 SQL。
如何在考虑关系的情况下比较相同 class 节点的相同 属性 值?我已经用谷歌搜索了,但没有找到答案。
尽量不要贬低我太多,我才刚开始学这东西。 提前致谢。
所以,我认为这个查询应该有效:
// 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
下面是一个简单的查询,用于查找参加同一课程的所有具有相同姓氏的学生:
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;