两个简单的Cypher查询合并后执行失败问题求助
Hey there! It sounds like you ran into a snag when combining two simple Cypher queries against your Simpson family graph, but you didn’t share the exact combined query you tried. Let’s break down common reasons merged Cypher statements fail, using your existing graph structure as context to help you fix this.
Common Pitfalls & Fixes
1. Missing or Misplaced Semicolons
Cypher requires semicolons to separate independent statements. If you just glued two queries together without a semicolon, the interpreter might treat them as a single invalid statement.
Example of a broken query:
MATCH (l:Daughter)-[:owns]->(p:Pet) RETURN l.name, p.name MATCH (h:Father)-[:has]->(i:Item) RETURN h.name, i.name
Fixed version (separate statements):
MATCH (l:Daughter)-[:owns]->(p:Pet) RETURN l.name, p.name; MATCH (h:Father)-[:has]->(i:Item) RETURN h.name, i.name
Or, if you want to combine results into a single output, use UNION (make sure return columns match):
MATCH (l:Daughter)-[:owns]->(p:Pet) RETURN l.name AS entity, p.name AS related_entity UNION MATCH (h:Father)-[:has]->(i:Item) RETURN h.name AS entity, i.name AS related_entity
2. Conflicting Variable Bindings
If you reuse the same variable name across merged statements without resetting the context, Cypher will carry over the variable’s existing value, leading to unexpected results or errors.
Example of a problematic query:
MATCH (n)-[:has]->(i:Item) WHERE n.name = "Lisa" RETURN i.name; MATCH (n)-[:owns]->(p:Pet) RETURN n.name, p.name
Here, the second MATCH will only use n bound to Lisa from the first query, not look for all nodes with an owns relationship.
Fix: Use unique variable names or reset the context with WITH:
MATCH (n)-[:has]->(i:Item) WHERE n.name = "Lisa" RETURN i.name; MATCH (person)-[:owns]->(pet) RETURN person.name, pet.name
3. Unfinished Initial Graph Creation
Looking at your initial CREATE statements, the last line cuts off:
CREATE (lisa)-[:has]->(:Item {name: "s...
If this statement was never completed when you built your graph, you might have a malformed node or relationship, which could cause subsequent queries to behave unexpectedly. Make sure to finish this statement (e.g., CREATE (lisa)-[:has]->(:Item {name: "saxophone"})) and re-run the full graph setup if needed.
4. Incorrect Use of Combining Clauses
If you’re trying to fetch related data in a single query instead of running two separate ones, avoid just stacking MATCH clauses without proper chaining. For example, if you want to get Lisa’s pet and her items in one go, chain the matches:
MATCH (l:Daughter {name: "Lisa"})-[:owns]->(p:Pet), (l)-[:has]->(i:Item) RETURN l.name, p.name, i.name
Next Step
If you share the exact merged query that failed, I can give you a precise fix tailored to what you’re trying to achieve!
内容的提问来源于stack exchange,提问作者Erik G

