【发布时间】:2018-09-14 07:54:36
【问题描述】:
我有 2 个帐户节点和几个列表节点,如下所示。 Match 语句导致显示 2 个帐户,并与与该帐户关联的每个列表都有关系。
我想要做的是基于至少 1 个共享相同电话号码的列表在两个帐户之间建立关系。
如果可能的话,我想查看绘制的两个帐户节点之间的关系以及两个列表之间的关系,只要列表来自不同的帐户。
MERGE (:Account {account_id:11})
MERGE (:Listing {account_id:11, listing_id:1001, phone:99468320, author:'Paul'})
MERGE (:Account {account_id:12})
MERGE (:Listing {account_id:12, listing_id:1002, phone:97412521, author:'Sam'})
MERGE (:Listing {account_id:12, listing_id:1003, phone:97412521, author:'Sam'})
MERGE (:Listing {account_id:12, listing_id:1004, phone:99468320, author:'Sam'})
MERGE (:Listing {account_id:12, listing_id:1004, phone:0, author:'Same'})
MATCH (a:Account),(l:Listing)
WHERE a.account_id = l.account_id
CREATE (a)-[:LISTING]->(l)
RETURN a,l;
对于后者,我确实尝试了以下方法,但它有点疯狂,因为它将具有相同编号的每个列表相互关联,只有在 account_id 不同时才这样做。
match (p1:Listing)
with p1
match (p2:Listing)
where p2.phone = p1.phone and p1 <> p2
merge(p1)-[r:SHARED_PHONE]-(p2)
RETURN p1, p2
【问题讨论】:
-
你为什么不把
p1 <> p2替换成p1.account_id <> p2.account_id呢?根据定义,如果帐户 id 不匹配,则它们不是同一个节点。 (或NOT p1.account_id = p2.account_id,如果您希望它更具可读性)另外,这将创建 2 个关系,每个方向一个。您确定不想将电话号码拆分到他们自己的节点以使您更轻松地跟踪 Cyphers 吗?