如何删除neo4j中的重复行

klr1opcd  于 2022-10-21  发布在  其他
关注(0)|答案(1)|浏览(230)

在NEO4j中,我的查询中有一行使用了cypher:

  1. "WITH *, collect(human.name) as sample"

这会产生如下结果:
回答

Bob
Sally
Bob
Bob
Sally
我想去掉重复的部分,这样答案就会产生:
回答

Bob
Sally
我该如何着手做这件事呢?下面的代码行实现了我想要的结果,但它不允许我在返回之后继续编写其他查询行,因为“只能在查询的最后一行使用返回”。

  1. RETURN DISTINCT sample
nom7f22z

nom7f22z1#

正如您提到的,DISTINCT可以完成这项工作,请使用:

  1. WITH collect(DISTINCT human.name) as sample

例如,对于示例数据:

  1. MERGE (a:Human{name: "Bob", key: 1})
  2. MERGE (c:Human{name: "Sally", key: 2})
  3. MERGE (b:Human{name: "Bob", key: 3})
  4. MERGE (d:Human{name: "Bob", key: 4})
  5. MERGE (e:Human{name: "Sally", key: 5})

和查询:

  1. MATCH (human:Human)
  2. WITH COLLECT (DISTINCT human.name) as sample
  3. return sample

回应是:

  1. ╒═══════════════╕
  2. "sample"
  3. ╞═══════════════╡
  4. │["Bob","Sally"]│
  5. └───────────────┘
展开查看全部

相关问题