Neo4j cypher语法简单使用

create:创建, match:查询,return:返回值

一、create的使用

(1)创建单个结点
   create (n) return n
(2)创建带标签的结点
   create (n:Person) return n
(3)创建多个结点
   create (n:Person), (m:Friend) return n, m或create (n:Person:Student) return n
(4)创建带标签、属性的结点
   create (n:Person {name:“张三”, age:23, sex:“男”}) return n
(5)创建关系
   match (n:Person{name:“张三”}), (m:Person{name:“李四”}) create (n)-[r:Knows{time:1994}]->(m) retur r
   start a =node(0),b=node(1) create (a)-[r:Knows]->(b) return r

二、match的使用

【where用法:match (n) where n:Person/n.name=“zhangsan” return n】
结点标签labels()、关系类型type()、属性值properties()
match (n:person)-[r:Knows]->(m:person) where n.name=“小明” return labels(n), m, type®, properties® limit 2

(1)查询所有结点
  match (n) return n
(2)查询指定的结点
  match (n:Person{name:“张三”}) return n
(3)根据属性查询
  MATCH (n{name:“张三”}) RETURN n
(4)根据标签和属性查询
  MATCH (n:Person{name:“李四”}}) RETURN n

(5)关系查询
  查询张三认识的人:Match (n:people{name: ‘小明’})-[:gift]->(end:people) return end
  查询与张三有关系的人:Match (n:people{name: ‘小明’})-[:gift]-(end:people) return end

(6)层级查询
  查询第二层结点:Match (start:people{name:’小明’})-[:gift2…2]->(end:people) return end
  查询第一、二层的结点:Match (start:people{name:’小明’})-[:gift
2…2]->(end:people) return end
  查询第直接与间接关系的结点Match (start:people{name:’小明’})-[:gift*]->(end:people) return end

(7)LIMIT查询标签(people)中的10个节点
  Match (n:people) return n limit 10
(8)distinct查询标签(people)中所有的不同的age
  Match (n:people) return distinct(n.age)
(9)根据name排序
  match (n) return n.name order by n.name(默认)
  match (n) return n.name order by n.name asc(升序)
  match (n) return n.name order by n.name desc(降序)

(10)IN 查询id为21,40,102的结点
  Match (n) where ID(n) IN[21,40,102] return n
(11)With以…开始/结尾
  查询name以‘小’开头的节点:Match (n) where n.name starts with ‘小’ return n
  查询name以‘明’结尾的节点:Match (n) where n.name ends with ‘明’ return n

(12)Count查询标签people的结点个数:
  Match (n:people) return count(n)

(13)Contains 统计名字含有"张"结点有哪些
  Match (n) where n.name Contains ‘张’ return n
(14)Union all 求并集
  不去重:Match(n:People) where n.age>16 return n.name as name Union all Match (n:friend) where n.age=18 return n.name as name
  去重:Match (n:People) where n.age>16 return n.name as name Union Match(n:friend) where n.age=18 return n.name as name

三、set更新(修改)结点和关系的标签属性

(1)修改属性值
  start a = node(*) where a.name=“a” set a.name=“A” return a,a.name ;
(2)添加属性
  match(n{ name: ‘Peter’ }) set n+= { age: 21, sex: “男” }
(3)删除属性
  match (n:Person{name:“张三”}) set n.name=NULL return n
(4)在结点上加标签
  match (n{name:“张三”) set n:“家里” return n
  match (n{name:“张三”}) set n:“车子”:“房子” return n

四、DELETE删除结点和关系

(1)删除所有结点与关系
  start n=node(*) match n-[r]-() delete n,r;
  match (n) optional match (n)-[r]-() delete n,r

(2)删除结点
  match (n:Person) delete n
(3)删除结点和连接的关系
  match (n:Person{name:“张三”})-[r]-() delete n,r

五、remove移除标签和属性

(1)移除age属性
  match (n:Person{name:“张三”}) remove n.age return n
(2)移除结点标签
  match (n:Person{name:“张三”}) remove n:Person return n

你可能感兴趣的:(学习备忘,学习记录)