对于双向连接的节点,密码返回两次 [英] cypher return twice for nodes that connected in bi-directional relationship

查看:54
本文介绍了对于双向连接的节点,密码返回两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个节点:(A),(B),由[:FRIEND]连接

I have 2 nodes: (A), (B), connected by [:FRIEND]

当我运行以下命令时,

start n = node(*) match (n)-[r:FRIEND]-(b) return n.name, b.name;

它返回2行: A,B和B,A.

it returns 2 rows: A, B and B, A.

我想知道如何使它只返回一个记录,因为这种关系是双向的,所以A-[:FRIEND] -B和B-[:FRIEND] -A被认为是相同的结果.

I wonder, how to make it return only one record, because the relationship is bidirectional, A -[:FRIEND]-B and B-[:FRIEND]-A is considered same result.

谢谢.

推荐答案

一个窍门是在ID上添加where,以便您也以一致的顺序获得它们:

One trick is to add a where on the IDs, so you get them in a consistent order as well:

start n = node(*) 
match (n)-[r:FRIEND]-(b) 
where id(n) < id(b) 
return n.name, b.name;

http://console.neo4j.org/r/1ry0ga

如果它们之间有多个关系(例如,双向),则可以添加一个不同的修饰符以获得相同的结果:

If you have multiple relationships between them (in both directions, for example), you can add a distinct modifier to get the same results:

start n = node(*) 
match (n)-[r:FRIEND]-(b) 
where id(n) < id(b) 
return distinct n.name, b.name;

这篇关于对于双向连接的节点,密码返回两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆