GraphGists

使用 Cypher 和 Neoj4 进行练习

创建脚本

完整图

查询

基本查询

  • 返回动物园中所有雄性动物。

match (n:Animal{sexo:"Macho"})
return n

结果

  • 返回年龄超过 40 岁的员工人数。

match(empleado:Empleado)
where empleado.edad > 40
return count(empleado) as `Cantidad empleados`

结果

中级查询

  • 返回与 Tadala 共享同一位兽医的所有动物。

Match(animal:Animal{name:"Tadala"})<-[:SE_ENCARGA_DE]-(veterinario:Empleado{puesto:"Veterinario"})
With veterinario
Match (veterinario)-[:SE_ENCARGA_DE]->(animal2:Animal)
Where animal2.name <> "Tadala"
Return animal2

结果

  • 返回由名为 Clara 的员工负责的动物幼崽。

Match (cuidador:Empleado{name:"Clara"})-[:SE_ENCARGA_DE]->(recinto:Recinto)<-[:HABITA]-(cria:Animal)-[:ES_HIJO_DE]->(animal:Animal)
Return distinct(cria)

结果

  • 返回尚未指定兽医的幼崽。

Match (animal:Animal)<-[:ES_HIJO_DE]-(cria:Animal)
Where not (:Empleado{puesto:"Veterinario"})-[:SE_ENCARGA_DE]->(cria)
Return distinct(cria)

结果

高级查询

  • 返回由负责 Canela 的同一批饲养员照顾且居住在不同围栏中的动物。同时返回它们居住的围栏。

match (animal:Animal{name:"Canela"})-[:HABITA]->(recinto:Recinto)<-[:SE_ENCARGA_DE]-(cuidador:Empleado{puesto:"Cuidador"})
with animal, recinto, cuidador
match (animal2:Animal)-[:HABITA]->(recinto2:Recinto)<-[:SE_ENCARGA_DE]-(cuidador)
where animal2.name <> "Canela"
and recinto <> recinto2
return distinct(animal2), recinto2

结果

  • 返回有伴侣、指定兽医姓名包含字母“o”且居住在可用状态围栏中的动物物种的平均年龄。

match (recinto:Recinto)<-[:HABITA]-(animal:Animal)-[:ES_PAREJA_DE]->(animal2:Animal)-[:HABITA]->(recinto:Recinto)
where recinto.estado = "Disponible"
with animal, animal2
match (veterinario:Empleado{puesto:"Veterinario"})
where((animal)<-[:SE_ENCARGA_DE]-(veterinario)
or (animal2)<-[:SE_ENCARGA_DE]-(veterinario))
and veterinario.name CONTAINS 'o'
return distinct labels(animal)[1] as Especie, avg(animal.edad) as `Edad media`

结果

© . All rights reserved.