title: Neo4j, Cypher: Conditional Create
body: <p>I have a specific problem to make a conditional create statement. I can't use <code>MERGE</code> here since the <code>MATCH</code> AND <code>CREATE</code> statement are different. Here is the query..</p>
  
  <pre><code>....
  WITH m
  OPTIONAL MATCH (x)-[r1:FeedNext{user_id:USER_ID}]-&gt;(m)-[r2:FeedNext{user_id:USER_ID}]-&gt;(y) 
  WHERE x IS NOT NULL AND y IS NOT NULL 
  WITH x, y, m, r1, r2
  CREATE (x)-[:FeedNext{user_id:USER_ID}]-&gt;(y) 
  DELETE r1, r2 
  RETURN m 
  </code></pre>
  
  <p>returns 0 rows For the case when <code>x</code> and <code>y</code> are <code>null</code>.</p>
  
  <p>While <code>m</code> is not returned  <code>null</code> for the same case on running..</p>
  
  <pre><code>....
  WITH m
  OPTIONAL MATCH (x)-[r1:FeedNext{user_id:USER_ID}]-&gt;(m)-[r2:FeedNext{user_id:USER_ID}]-&gt;(y) 
  RETURN m 
  </code></pre>
  
  <p>What's the way around, to return the value of <code>m</code> in all cases?</p>