我的图表中有一个人员节点:
CREATE (:Person {id: "1" name:"foo"})
可以选择将一个或多个电话号码与之关联。然后我执行以下操作来查询此人:
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
当此人拥有电话号码时,此功能正常:
"1", "foo", [["p1", "111-1111"], ["p2", "111-1112"]]
但如果此人没有任何电话号码,我会得到以下结果:
"1", "foo", [[null, null]]
如果没有电话号码,如何返回以下内容?
"1", "foo", null
答案 0 :(得分: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