Neo4j简单`CASE`表达

时间:2017-06-19 13:48:07

标签: neo4j cypher

我正在尝试使用neo4j CASE构造生成一个表,其中包含发件人的名称,接收者的名称以及发件人作为计数发送的消息数(如果有的话,否则为0)但我我没有得到预期的结果。这是我的疑问:

MATCH(e:Employee{key:1}),(b:Employee), 
OPTIONAL MATCH (e)-[r:Message]->(b)
RETURN e.name, DISTINCT b.name,
CASE
WHEN (e)-[r:Message]->(b)
THEN COUNT(r)
ELSE 0 END AS Messages

我在这里做错了什么?提前谢谢!

1 个答案:

答案 0 :(得分:2)

I believe you do not need this CASE WHEN to achieve your goal. If you need the count() of :Message relationships between e and b nodes you can simply return the count of it:

MATCH(e:Employee{key:1}),(b:Employee)
OPTIONAL MATCH (e)-[r:Message]->(b)
RETURN DISTINCT e.name, b.name, COUNT(r) as Messages

This query will return zero in the COUNT(r) if no relationship exists between e and b nodes.