What does a comma in a Cypher query do?

前端 未结 2 1704
-上瘾入骨i
-上瘾入骨i 2020-12-02 00:24

A co-worker coded something like this:

match (a)-[r]->(b), (c) set c.x=y

What does the comma do? Is it just shorthand for MATCH?

2条回答
  •  孤城傲影
    2020-12-02 00:39

    Brian does a good job of explaining how the comma can be used to construct larger subgraph patterns, but there's also a subtle yet important difference between using the comma and a second MATCH clause.

    Consider the simple graph of two nodes with one relationship between them. The query

    MATCH ()-->() MATCH ()-->() RETURN 1
    

    will return one row with the number 1. Replace the second MATCH with a comma, however, and no rows will be returned at all:

    MATCH ()-->(), ()-->() RETURN 1
    

    This is because of the notion of relationship uniqueness. Inside each MATCH clause, each relationship will be traversed only once. That means that for my second query, the one relationship in the graph will be matched by the first pattern, and the second pattern will not be able to match anything, leading to the whole pattern not matching. My first query will match the one relationship once in each of the clauses, and thus create a row for the result.

    Read more about this in the Neo4j manual: http://neo4j.com/docs/stable/cypherdoc-uniqueness.html

提交回复
热议问题