What is the difference between multiple MATCH clauses and a comma in a Cypher query?

后端 未结 5 682
死守一世寂寞
死守一世寂寞 2021-02-01 02:43

In a Cypher query language for Neo4j, what is the difference between one MATCH clause immediately following another like this:

MATCH (d:Document{document_ID:2})
         


        
5条回答
  •  北海茫月
    2021-02-01 03:09

    I think it's better to explain providing an example when there's a difference. Let's say we have the "Movie" database which is provided by official Neo4j tutorials. And there're 10 :WROTE relationships in total between :Person and :Movie nodes

    MATCH (:Person)-[r:WROTE]->(:Movie) RETURN count(r); // returns 10
    

    1) Let's try the next query with two MATCH clauses:

    MATCH (p:Person)-[:WROTE]->(m:Movie) MATCH (p2:Person)-[:WROTE]->(m2:Movie)
    RETURN p.name, m.title, p2.name, m2.title;
    

    Sure you will see 10*10 = 100 records in the result.

    2) Let's try the query with one MATCH clause and two patterns:

    MATCH (p:Person)-[:WROTE]->(m:Movie), (p2:Person)-[:WROTE]->(m2:Movie) 
    RETURN p.name, m.title, p2.name, m2.title;
    

    Now you will see 90 records are returned. That's because in this case records where p = p2 and m = m2 with the same relationship between them (:WROTE) are excluded.

    For example, there IS a record in the first case (two MATCH clauses)

    p.name m.title p2.name m2.title

    "Aaron Sorkin" "A Few Good Men" "Aaron Sorkin" "A Few Good Men"

    while there's NO such a record in the second case (one MATCH, two patterns)

提交回复
热议问题