如何使用neo4j执行多个左外连接

时间:2017-03-30 11:02:20

标签: neo4j

如何在neo4j中创建多个关系以执行左外连接

我在sql中的查询如下所示

Select SI.SendId,MC.CampaignName,SI.Name,
SI.SendDate,G.Name As GName,C.Name as FN From SendItem SI 
LEFT OUTER JOIN Campaign MC ON MC.CampaignId=SI.MessageCampaignId 
LEFT OUTER JOIN Groups G ON G.Id=SI.GroupId
LEFT OUTER JOIN Contact C ON C.ContactId=SI.Contact
order by SI.SendDate desc

我是neo4j的新手,所以请任何人帮我创建关系以在neo4j中执行上述查询

1 个答案:

答案 0 :(得分:4)

将表视为具有标签的节点,并将连接视为关系:

查询创建:

// Create campaign
MERGE (MC:Campaign {name: 'CampaignName'})

// Create group
MERGE (G:Groups {name: 'GroupsName'})

// Create contact
MERGE (C:Contact {name: 'ContactName'})

// Create item 
MERGE (SI:SendItem {name: 'SendItemName', sendDate: timestamp()})

// Create relationships
MERGE (SI)-[:hasContact]->(C)
MERGE (SI)-[:inGroup]->(G)
MERGE (SI)-[:inCampaign]->(MC)

查询获取:

MATCH (SI:SendItem)
WITH SI
     OPTIONAL MATCH (SI)-[:hasContact]->(C:Contact)
     OPTIONAL MATCH (SI)-[:inGroup]->(G:Group)
     OPTIONAL MATCH (SI)-[: inCampaign]->(MC:Campaign)
RETURN ID(SI) as id,
       SI.name as sendItemName,
       SI.sendDate as date,
       C.name as contactName,
       G.name as groupName,
       MC.name as campaignName
ORDER BY SI.sendDate desc