简体   繁体   中英

Cypher: Query to select two nodes connected by two instances of the same relation with different attributes?

I have some node n8 connected to another node n9 by two instances of one and the same relation. These instances differ only by relation attribute tag . First relation instance have tag = foo and second instance has tag = bar (see code below).

What query will select these two nodes from all other nodes and relations? In other words: how to query two nodes connected by two instances of the same relation with different attributes ?

create (n8 { id:'n8' })
create (n9 {id:'n9' })

MATCH (x),(y)
WHERE x.id = 'n8' AND y.id = 'n9'
CREATE (x)-[r:rl {tag:'foo'}]->(y)
RETURN r

MATCH (x),(y)
WHERE x.id = 'n8' AND y.id = 'n9'
CREATE (x)-[r:rl {tag:'bar'}]->(y)
RETURN r

This should do the trick. Use two MATCH clauses, paired with a WHERE clause that permits the relationship's tag to be any value in a list of possibilities:

MATCH (n8)-[firstRel:r1]->(n9),
      n8-[secondRel:r1]->n9
WHERE firstRel.tag in ['foo', 'bar'] AND
      secondRel.tag in ['foo', 'bar'] AND
      firstRel <> secondRel AND
      firstRel.tag <> secondRel.tag
return firstRel, secondRel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM