我正在尝试使用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
我在这里做错了什么?提前谢谢!
答案 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.