问题
I have User and Building labels for nodes, the relationships are User-[:HAS_PERMISSION]->Building
To get all buildings that a user is having permissions to so I use MATCH (u:User {id: {id}}),(u)-[r:HAS_PERMISSION]->(b:Building) return b
How can I just return the user in case he exists and there are no relationships? (basically I also want to know if the user exists at all..)
回答1:
Not sure if I understand your question correctly, but I think you'll need an OPTIONAL MATCH
. This query shows all users with or without access to a building.
MATCH (u:User)
OPTIONAL MATCH (u)-[r:HAS_PERMISSION]->(b:Building)
RETURN u, b
Or maybe your requirement is simpler. If you just want all users, there's no need to use pattern matching:
MATCH (u:User)
RETURN u
来源:https://stackoverflow.com/questions/22484598/cypher-how-to-return-a-node-if-the-relationship-doesnt-exist