2

I have a person node in my graph:

CREATE (:Person {id: "1" name:"foo"})

Which can optionally have one or more phone numbers associated with it. I then do the following to query the person:

MATCH (p:Person)
WHERE p.id = "1"
WITH p
MATCH p-[?:PHONE]->ph
RETURN p.id, p.name, COLLECT([ph.id, ph.number]) AS phones

This works fine when the person has phone numbers:

"1", "foo", [["p1", "111-1111"], ["p2", "111-1112"]]

But in the case that the person doesn't have any phone numbers, I get the following result:

"1", "foo", [[null, null]]

How can I return the the following instead if there are no phone numbers?

"1", "foo", null
4

1 回答 1

4

您可以使用“CASE”表达式为这两种情况返回两个不同的结果,

MATCH (p:Person)
WHERE p.id = "1"
WITH p
MATCH p-[?:PHONE]->ph
WITH p.id as pid, p.name as pname, collect([ph.id, ph.number]) as phones
RETURN pid, pname, CASE WHEN all ( x in head(phones) where x = NULL ) THEN  NULL ELSE phones END
于 2013-09-13T13:10:38.000 回答