我用下面轉載您的示例數據集命令:
CREATE
(a1:A {name:'a1'}),
(a2:A {name:'a2'}),
(a3:A {name:'a3'}),
(a4:A {name:'a4'}),
(a5:A {name:'a5'}),
(a6:A {name:'a6'}),
(a7:A {name:'a7'}),
(a1)-[:R]->(b1:B {name: 'b1', property: 'p1'}),
(a1)-[:R]->(b2:B {name: 'b2', property: 'p2'}),
(a1)-[:R]->(b3:B {name: 'b3', property: 'p3'}),
(a2)-[:R]->(b4:B {name: 'b4', property: 'p4'}),
(a3)-[:R]->(b5:B {name: 'b5', property: 'p2'}),
(a3)-[:R]->(b6:B {name: 'b6', property: 'p4'}),
(a4)-[:R]->(b7:B {name: 'b7', property: 'p2'}),
(a4)-[:R]->(b8:B {name: 'b8', property: 'p4'}),
(a5)-[:R]->(b9:B {name: 'b9', property: 'p4'}),
(a6)-[:R]->(b10:B {name: 'b10', property: 'p1'}),
(a7)-[:R]->(b11:B {name: 'b11', property: 'p3'}),
(a7)-[:R]->(b12:B {name: 'b12', property: 'p5'})
該查詢返回所期望的對:
MATCH (a1:A)-[:R]->(b1:B) // 1
WITH *
ORDER BY ID(b1)
WITH a1, collect(b1.property) AS b1Properties // 2
MATCH (a2:A)-[:R]->(b2:B) // 3
WHERE ID(a1) < ID(a2) // 4
WITH *
ORDER BY ID(b2)
WITH a1, b1Properties, a2, collect(b2.property) AS b2Properties // 5
WHERE b1Properties = b2Properties // 6
RETURN a1, a2, b1Properties, b2Properties // 7
說明:
- 我們查詢
a1:A
- b1:B
對,由b1
節點(確保b1
s爲總的標識分類以相同的順序)。
- 我們將
b1
節點的property
值收集到列表b1Properties
。
- 我們做同樣的
a2:A
- b2:B
對,
- 但做
ID(a1) < ID(a2)
過濾以
- 排除同一節點對(例如
a6, a6
)
- 排除重複對(例如
a6, a7
和a7, a6
)。
b2
節點的property
值進行排序並收集到b2Properties
列表。
- 我們只保留
b1Properties
和b2Properties
相等的結果。
- 我們返回結果。
的WITH *
線都需要使用ORDER BY
子句,它必須由一個WITH
或RETURN
子句之前。
結果是:
╒══════════╤══════════╤════════════╤════════════╕
│a1 │a2 │b1Properties│b2Properties│
╞══════════╪══════════╪════════════╪════════════╡
│{name: a3}│{name: a4}│[p2, p4] │[p2, p4] │
├──────────┼──────────┼────────────┼────────────┤
│{name: a2}│{name: a5}│[p4] │[p4] │
└──────────┴──────────┴────────────┴────────────┘