简体   繁体   中英

Neo4J Trying to find all nodes with matching property values

I have a DB with students and courses that they're attending. The objective is to find all students with the same lastname that attent the same courses and return their firstNames and courseNames. (s:Student)-[r:ENROLLEDIN]->(c:Course)

I've tried a bunch of stuff out of my head but I believe I'm still thinking SQL.

How do I compare the same property values across the nodes of the same class taking into account a relation? I've googled it, no answer found though.

Try not to minus me too much, I've just started learning this thing. Thanks in advance.

Below is a simple query to find all students with the same last name that attend the same course :

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;

So, I think this query should work:

// 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM