jas
jas

Reputation: 1582

case statement to return node property and true if relationship exist

I want to return true or false if relationship exist between between two nodes and also return a property if rel exist otherwise return only false. I tried this query - It works when rel exist but doesn't return anything if rel doesn't exist

MATCH (n:User {username: 'user'})-[r:HAS_CAR]-(m:Car) 
RETURN SIGN(COUNT(r)), CASE SIGN(COUNT(r)) WHEN 1 THEN m.name END as name

Upvotes: 1

Views: 1760

Answers (1)

Stefan Armbruster
Stefan Armbruster

Reputation: 39915

You need to use an OPTIONAL MATCH to cover the case when no relationship exists:

MATCH (n:User{namename:'user'})
OPTIONAL MATCH (n)-[r:HAS_CAR]->(m:Car)
RETURN n, case when count(m)>0 then collect(r.roles) else false end

I've used collect to return only one row even if the user has multiple cars.

Upvotes: 2

Related Questions