2013-05-09 29 views
6

我想將兩個請求組合成一個查詢,我不確定在單個密碼查詢中使用2個匹配語句時會發生什麼。如何在密碼查詢中使用兩條匹配語句

說我有一個朋友的列表,我希望看到我的朋友列表中的每個他們的叔叔和兄弟姐妹列在一個集合。我可以有兩個可以完成這項工作的比賽陳述嗎?例如

match friends-[:childOf]->parents-[:brother]->uncles 
    , friends-[:childOf]->parents<-[:childOf]-siblings 
return friends, collect(siblings), collect(uncles) 

但是,如果我做這樣的查詢,它總是返回沒有結果。

+2

你能通過http://console.neo4j.org/分享一個示例圖嗎? – 2013-05-09 15:43:52

回答

7

既然你已經在你的第一場比賽類選擇父母,你可以這樣做 -

match friends-[:childOf]->parents-[:brother]->uncles 
with friends, parents, uncles 
match parents<-[:childOf]-siblings 
return friends, collect(siblings), collect(uncles) 
1

你可能想使一些可選的關係。例如,如果找到兄弟但沒有找到任何叔叔,則該查詢將返回null,因爲它不滿足兩個匹配子句。如果您將結束關係設爲可選,那麼您不必完全滿足這兩個條款以返回數據。所以:

match friends-[:childOf]->parents-[?:brother]->uncles 
    , friends-[:childOf]->parents<-[?:childOf]-siblings 
return friends, collect(siblings), collect(uncles)