# Multiple relationships with one query, loop-function?!?

**URL:** <https://community.neo4j.com/t/multiple-relationships-with-one-query-loop-function/33928>\
**Category:** Newbie Questions\
**Tags:** relationship\
**Created:** [February 18, 2021, 4:39pm UTC](https://community.neo4j.com/t/multiple-relationships-with-one-query-loop-function/33928 "2021-02-18T16:39:04Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![dionisios95](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/dionisios95/32/4090_2.png) [@dionisios95](https://community.neo4j.com/u/dionisios95)\
**Post date:** [February 18, 2021, 4:39pm UTC](https://community.neo4j.com/t/multiple-relationships-with-one-query-loop-function/33928/1 "2021-02-18T16:39:04Z")

</div>

## Hello, I have an issue and I hope you can help me. I have imported some csv-Files to create nodes and it worked. They include just generic simple data. For example: ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/1/c/1c30c6a43f70173b8d4d8d7e83aa1b7e0c14b43b.png)

![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/1/2/120b540f04b8bae72c63a9f1bb992eca32cb1ffd.png)  
Now I want to create multiple relationships with just one query. Of course I can do it like that ...

 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/6/2/62a7b80229e7d18db2fb9ae2e722667f0696c276.png)  
but I want something like an loop, that works incremently depend on the ID`s.  
For example (pid=1) on (aid=10) (pid=2) on (aid=11) and so on.  
Is there an opportunity to do that just with cypher-code or any tools ?

---

<div class="post-metadata">

**Author:** ![dana\_canzano](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/dana_canzano/32/27392_2.png) [@dana\_canzano](https://community.neo4j.com/u/dana_canzano)\
**Post date:** [February 18, 2021, 7:47pm UTC](https://community.neo4j.com/t/multiple-relationships-with-one-query-loop-function/33928/2 "2021-02-18T19:47:00Z")

</div>

depending on how large of a loop but one could

```auto
unwind [1,2,3,4,5,6,7,8,9,10] as loop
match (p1:Person {pid: loop}), (a1:Address {aid:10+loop})  
create (p1)-[:LIVING_IN]->(a1);

```

though in the above the first pass would evaluate to {pid: 1} and {aid: 11} , the 2nd would be {pid: 2} and {aid: 12} and isnt exactly what you want so maybe {aid: 10+loop} should be {aid:9+loop} so as to get {pid: 1} and {aid: 10} and the 2nd pass of {pid: 2} and {aid: 11} etc. Simply an example

---

<div class="post-metadata">

**Author:** ![dionisios95](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/dionisios95/32/4090_2.png) [@dionisios95](https://community.neo4j.com/u/dionisios95)\
**Post date:** [February 18, 2021, 8:12pm UTC](https://community.neo4j.com/t/multiple-relationships-with-one-query-loop-function/33928/3 "2021-02-18T20:12:20Z")

</div>

Interesting, but I have round about 200 pid`s that have to be related. This could work for my other nodes with less datasets.

---

<div class="post-metadata">

**Author:** ![dana\_canzano](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/dana_canzano/32/27392_2.png) [@dana\_canzano](https://community.neo4j.com/u/dana_canzano)\
**Post date:** [February 18, 2021, 8:50pm UTC](https://community.neo4j.com/t/multiple-relationships-with-one-query-loop-function/33928/4 "2021-02-18T20:50:29Z")

</div>

ah try

```auto
foreach ( loop in range (1,200) | match (p1:Person {pid: loop}), (a1:Address {aid:10+loop}) create (p1)-[:LIVING_IN]->(a1);

```

---

<div class="post-metadata">

**Author:** ![dionisios95](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/dionisios95/32/4090_2.png) [@dionisios95](https://community.neo4j.com/u/dionisios95)\
**Post date:** [February 19, 2021, 12:45am UTC](https://community.neo4j.com/t/multiple-relationships-with-one-query-loop-function/33928/5 "2021-02-19T00:45:35Z")

</div>

Your first recommendation unfortunately does not create anything:

 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/f/e/fecfbaad4fcd24f240813670daabac1dc3cd6e48.png)

The loop-Version does not work, because of the syntax I guess.

 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/8/9/89fb057f095f5396c2f86497a66ff2254618a2f8.png)

So I tried that with a match before the foreach, but now it wants to create an existing node. I just want the combining by a new relation :(

 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/3/5/35c55088fc07b43fe0d09e33a7ee67fda2fdea3d.png)

---

<div class="post-metadata">

**Author:** ![dana\_canzano](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/dana_canzano/32/27392_2.png) [@dana\_canzano](https://community.neo4j.com/u/dana_canzano)\
**Post date:** [February 19, 2021, 12:19pm UTC](https://community.neo4j.com/t/multiple-relationships-with-one-query-loop-function/33928/6 "2021-02-19T12:19:07Z")

</div>

regarding the syntax error my mistake. you need one final trailing ')' at the end of the line and thus as well as my mistake again in that you can not do a `create` within a `foreach`. However you can `merge` and presumably accomplish the same as originally requested and

```auto
foreach ( loop in range (1,200) | merge (p1:Person {pid: loop})-[:LIVING_IN]->(a1:Address {aid:10+loop}) ) 

```

and so the above will find a  
:Person node at pid=loop  
:Address node at aid:10+loop

if they dont exist, create said nodes and then also create a relationship between said nodes. If the :Person and :Address nodes already/previously exist then we will not create additional nodes, and the same for the relationship as well

---

<div class="post-metadata">

**Author:** ![dionisios95](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/dionisios95/32/4090_2.png) [@dionisios95](https://community.neo4j.com/u/dionisios95)\
**Post date:** [February 19, 2021, 3:07pm UTC](https://community.neo4j.com/t/multiple-relationships-with-one-query-loop-function/33928/7 "2021-02-19T15:07:59Z")

</div>

I got the same error-message with merge.

 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/6/2/62bc3aa04ae992575d472810557931dd44183c25.png)  
Note, that I have node-key-constraints on pid & aid ...  
When I drop them and do the query with merge I will get new nodes AND relations.  
Thats not what I am looking for.

---

<div class="post-metadata">

**Author:** ![dionisios95](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/dionisios95/32/4090_2.png) [@dionisios95](https://community.neo4j.com/u/dionisios95)\
**Post date:** [February 19, 2021, 3:44pm UTC](https://community.neo4j.com/t/multiple-relationships-with-one-query-loop-function/33928/8 "2021-02-19T15:44:43Z")

</div>

I also tried with set:

 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/0/a/0a24959d9dfa6f84edd009f35acf0db14eb48793.png)  
Why does he try to create a new node?  
I mean, if I usually do this, it works. :  
 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/7/5/75144a3d9b3054f323de7a3105c29e26c446089a.png)

---

<div class="post-metadata">

**Author:** ![ameyasoft](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/ameyasoft/32/32639_2.png) [@ameyasoft](https://community.neo4j.com/u/ameyasoft)\
**Post date:** [February 20, 2021, 5:41am UTC](https://community.neo4j.com/t/multiple-relationships-with-one-query-loop-function/33928/9 "2021-02-20T05:41:26Z")

</div>

```auto
Try this:

unwind [1,2,3,4,5,6,7,8,9,10] as ids
merge (p:Person {pid:ids})
merge (a:Address {aid: ids + 10})
merge (p)-[:HAS_ADDRESS]->(a)
return p, a

```

Result:

 ![Screen Shot 2021-02-19 at 9.32.40 PM](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/3/0/305ad4738f7291816ee001e412ba90657df2fa40.png)

```auto
match (p:Person)-[:HAS_ADDRESS]->(a:Address)
return collect(p.pid) as pid, collect(a.aid) as aid

```

Result:

 ![Screen Shot 2021-02-19 at 9.35.58 PM](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/a/4/a4f7f968fc050810aaef4e29f4460d8c97dfcf64.png)

---

<div class="post-metadata">

**Author:** ![dana\_canzano](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/dana_canzano/32/27392_2.png) [@dana\_canzano](https://community.neo4j.com/u/dana_canzano)\
**Post date:** [February 20, 2021, 1:27pm UTC](https://community.neo4j.com/t/multiple-relationships-with-one-query-loop-function/33928/10 "2021-02-20T13:27:43Z")

</div>

and apologies for overthinking this but rather than

```auto
unwind [1,2,3,4,5,6,7,8,9,10] ... ...

```

or

```auto
foreach ( x in range (1,200) .... ....

```

you can also do

```auto
unwind range (1,200) as x ... .... .....

```

and so thus my previous post of

```auto
unwind [1,2,3,4,5,6,7,8,9,10] as loop
match (p1:Person {pid: loop}), (a1:Address {aid:10+loop})  
create (p1)-[:LIVING_IN]->(a1);

```

can become

```auto
unwind range (1,200) as loop
match (p1:Person {pid: loop}), (a1:Address {aid:10+loop})  
create (p1)-[:LIVING_IN]->(a1);

```

---

<div class="post-metadata">

**Author:** ![dionisios95](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/dionisios95/32/4090_2.png) [@dionisios95](https://community.neo4j.com/u/dionisios95)\
**Post date:** [February 20, 2021, 1:44pm UTC](https://community.neo4j.com/t/multiple-relationships-with-one-query-loop-function/33928/11 "2021-02-20T13:44:06Z")

</div>

Thank you, the last one helped :)
