neo4j图数据库cypher语法

CREATE (myroot:Entity {modelType:'target', name:"myroot", tableType:'table'})
CREATE (myroot_myjh:attribute {name:'myjh'})
CREATE (myroot_myjd:attribute {name:'myjd'})
CREATE (myroot_myjh)-[:att]->(myroot),//该语句不能按想象地建立上面两个变量之间的关系,上面语句的变量不能传递到下面
重复执行会重复创建节点

match(n) return n; //查询所有节点和边
match(n:Entity) return n;//查询所有节点
match(n) where n.name IS NULL return n 
match(n) where n.name IS NULL delete n //需要先删除关系
MATCH ()-[r:attribute]-() return r;
MATCH ()-[r:attribute]-() delete r;
match(n) where n.name is null return n;

match (p)-[r]-(c) where c.name is null return p,r,c
match (p)-[r]-(c) where c.name is null 
delete r,c

match(a:Entity) where a.name="myroot"
match(b:attribute) where b.name='myjd'
CREATE (a)-[:att]->(b)

match(a:Entity) where a.name="myroot"
match(b:attribute) where b.name='myjh'
CREATE (a)-[:att]->(b)
MATCH ()-[r:att]-() delete r;

//下面的报错
match(a:Entity) where a.name="myroot"
match(b:attribute) where b.name='myjd'
CREATE (a)-[:att]->(b)
match(c:attribute) where c.name='myjh'
CREATE (a)-[:att]->(c)

错误信息:
WITH is required between CREATE and MATCH (line 4, column 1 (offset: 100))
"match(c:attribute) where c.name='myjh'"
 ^

//下面的正确
match(a:Entity) where a.name="myroot"
match(b:attribute) where b.name='myjd'
match(c:attribute) where c.name='myjh'
CREATE (a)-[:att]->(b)
CREATE (a)-[:att]->(c)
或
match(a:Entity) where a.name="myroot"
match(b:attribute) where b.name='myjd'
match(c:attribute) where c.name='myjh'
CREATE (a)-[:att]->(b),
 (a)-[:att]->(c)
 重复执行,就会重复建立关系
 
 //UNWIND、DISTINCT、collect
 UNWIND [1, 2, 3, null] AS x
RETURN x, 'val' AS y
 
 WITH [1, 1, 2, 2] AS coll
UNWIND coll AS x
WITH DISTINCT x
RETURN collect(x) AS setOfVals

WITH [[1, 2], [3, 4], 5] AS nested 
UNWIND nested AS x RETURN x

WITH [[1, 2], [3, 4], 5] AS nested
UNWIND nested AS x
UNWIND x AS y
RETURN y

WITH [] AS list
UNWIND
CASE
WHEN list = []
THEN [null]
ELSE list
END AS emptylist
RETURN emptylist
------------------------
Neo4j查询语句总结

1.如何找到一个节点x,x以某种关系同时连接两个不同节点a和b
match (a)-[r:relation]->(x)<-[r:relation]-(b) return x

2.如何找到节点a和b之间的最短路径
(1)match p=shortestpath((a)-[r:relation]-(b)) return nodes(p)
(2)match(n:na{name:’###’}),(m:nb{name:’###’})with n,m match p=shortestpath((n)-[r*…]-(m)) return p;

3.如何找到节点a和b之间以某种关系相连接的最短路径
p=shortestpath((a)-[r:relationname]->(b)) return nodes(p)

4.找到数据库中出现的唯一节点标签
match n return distinct labels(n)

5.找到数据库中出现的唯一关系类型
match n-[r]-() return distinct type(r)

6.找到数据库中的唯一节点标签和唯一关系类型
match n-[r]-() return distinct labels(n),type(r)

7.找到不与任何关系(或某种关系)向连的节点
start n = node() match n-[r:relationname]-() where r is null return n

8.找到某个带有特定属性的节点
start n=node() match n where has (n.someproperty) return n

9.找到与某种关系相连接的全部节点
start n= node() match n-[r:relationshipname]-() return distinct n

10.找到节点和它们的关系数,并以关系数目降序排列显示
start n=node() match n-[r]-() return n,count(r) as rel_count order by rel_count desc

11.返回图中所有节点的个数
start n = node() match n return count(n)

12.(1)删除图中关系:start n=node(*) match n-[r]-() delete r
(2)删除图中节点:start n =node(*) match n delete n
(3)删除图中所有东西:match (n) detach delete n

13.查询某类节点下某属性为特定值的节点
match (n:person)where n.name=”alice” return n

14.with
Cypher中的With关键字可以将前步查询的结果作为后一步查询的条件,这个在我的工作中可是帮了大忙哈哈。下面是两个栗子。
(1)match(p:node_se)-[re:推理条件]->(q:node_se) where p.name=‘FEV1%pred’and p.value=’<30%’ WITH p,re,q match (q:node_se) <-[re2:推理条件]- (c:node_se)return p, re,q,re2,c
(2)match(p:node_patient)-[re:个人情况]->(q:node_se) where p.name=‘qwe’ WITH p,re,q match (q:node_se) -[re2:推荐方案]-> (c:node_se) where q.name=‘first’ WITH p, re,q,re2,c match (c:node_se)-[re3:方案细节]->(d:drugs) return p, re,q,re2,c,re3,d

15.查询符合条件的某个节点的id
match(p) where p.name = ‘***’ and p.value = ‘***’ return id(p)

16.直接连接关系节点进行多层查询
match(na:bank{id:‘001’})-[re1]->(nb:company)-[re2]->(nc:people) return na,re1,nb,re2,nc

17.可以将查询结果赋给变量,然后返回
match data=(na:bank{id:‘001’})-[re1]->(nb:company)-[re2]->(nc:company) return data

18.变长路径检索
变长路径的表示方式是:[*N…M],N和M表示路径长度的最小值和最大值。
(a)-[ *2]->(b):表示路径长度为2,起始节点是a,终止节点是b;
(a)-[ *3…5]->(b):表示路径长度的最小值是3,最大值是5,起始节点是a,终止节点是b;
(a)-[ *…5]->(b):表示路径长度的最大值是5,起始节点是a,终止节点是b;
(a)-[ *3…]->(b):表示路径长度的最小值是3,起始节点是a,终止节点是b;
(a)-[ *]->(b):表示不限制路径长度,起始节点是a,终止节点是b;

19.Cypher对查询的结果进行去重
栗:match(p:node_se)-[re]->(q)where re.name <> ‘and’ return distinct(re.name)
(注:栗子中的<>为Cypher中的操作符之一,表示‘不等于’)

20.更新节点的 labels
Neo4j中的一个节点可以有多个 label,返回所有节点的label:match (n) return labels(n)
修改节点的 label,可以先新加 label,再删除旧的label
match (n:label_old) set n:label_new remove n:label_old
match(n:label_new) return labels(n)

21.更新节点的属性
match(n:) set n.new_property = n.old_property remove n.old_proerty

先总结这些吧,感觉cypher还挺有意思的,项目中经常琢磨着要怎么写感觉像玩乐高一样。
以上。
————————————————
版权声明:本文为CSDN博主「樱桃小胖子同学」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_40771521/article/details/95936491

//0:准备删除所有内容,要求先删除关系,再删除节点,有关系的节点不能删除
match ()-[r]-() delete r
match (n) delete n

1.创建节点
CREATE (st101:Student {name:'张三', no:'101'})
CREATE (st102:Student {name:'李四', no:'102'})
CREATE (st103:Student {name:'王五', no:'103'})
CREATE 
	(c01:Course {name:'大数据存储与管理技术',credit:4}),
	(c02:Course {name:'大数据应用技术',credit:4}),
	(c03:Course {name:'数据库',credit:4})
CREATE 
	(t01:Teacher {name:'文老师', title:'教授'}),
	(t02:Teacher {name:'吕老师', title:'副教授'}),
	(t03:Teacher {name:'包老师', title:'教授'})
CREATE 
	(st101)-[:选修]->(c01),
	(st102)-[:选修]->(c01),
	(st102)-[:选修]->(c02),
	(st103)-[:选修]->(c01),
	(st103)-[:选修]->(c02),
	(st103)-[:选修]->(c03)
CREATE 
	(t01)-[:讲授]->(c01),
	(t02)-[:讲授]->(c02),
	(t03)-[:讲授]->(c03),
	(t04)-[:讲授]->(c04)
CREATE (:Teacher {name:'赵老师', 职称:'教授'}) //可以中文作属性,也可以用中文作节点名
CREATE (:Province {name:'黑龙江',abbreviation:'黑'})
CREATE (:Province {name:'河北',abbreviation:'冀'})

2.--以下三种创建关系的例子,请单独执行
MATCH (a:Student), (b:Province) WHERE a.name = '张三' AND b.name = '黑龙江'
CREATE (a)-[r:来自]->(b)

MATCH (c:Student {name:'李四'}), (d:Province {name:'黑龙江'}) 
CREATE (c)-[r:来自]->(d)

MATCH (c:Student {name:'王五'}), (d:Province {name:'河北'}) 
CREATE (c)-[r:来自]->(d)

//将单独创建一个王五,一个河北,一个关系
CREATE (:Student {name:'王五'})-[r:来自]->(:Province {name:'河北'})
//上述结果不是我们想要的,删除掉 
match (s:Student {name:'王五'})-[r:来自]->(p:Province {name:'河北'}) 
delete r,s,p

3.修改
MATCH (s:Student{name:'李四'})-[r1]-(b:Province) //将李四的籍贯改为河北
CREATE (s)-[r2:来自]->(:Province{name:'河北'}) //先创建一个新的,
DELETE r1 //删除原来的

4.各种查询
4.1 查询所有节点和边:
match (n) return (n)

match all=(p:Province)-[r]-(s:Student) return all //查询各学生的籍贯

MATCH all=(s:Student{name:'李四'})-[r]-(b:Province)  return all //查询某学生的籍贯

match (t:Teacher{name:'文老师'}), (p:Province{name:'河北'}), path=shortestpath((t)-[*..3]-(p))
return path //文老师教过的河北籍学生

match (t:Teacher{name:'文老师'})-[]-(s:Student)-[]-(p:Province{name:'河北'})
return s,t,p,(s)-[]-(t),(s)-[]-(p) //文老师教过的河北籍学生

CREATE (:省份 {name:'黑龙江',abbreviation:'黑'}) //可以用中文作标签,但无法查询
MATCH (p:省份) delete p 
----
//neo4j如何直接返回该节点周围三层的节点和关系
match data=(t:Teacher{name:'文老师'})-[rel*1..3]-(b) return t,rel,b
match data=(t:Teacher{name:'文老师'})-[rel*1..3]-(b) return t,rel,b
match (t:Teacher)-[rel*1..1]-(b) where t.name='文老师' return t,rel,b
--->
This feature is deprecated and will be removed in future versions.
Binding relationships to a list in a variable length pattern is deprecated. (Binding a variable length relationship pattern to a variable ('rel') is deprecated and will be unsupported in a future version. The recommended way is to bind the whole path to a variable, then extract the relationships:
	MATCH p = (...)-[...]-(...)
	WITH *, relationships(p) AS rel)

//文老师教过的黑龙江的学生的情况
match p=(t:Teacher)-[*]-(b:Province) 
where t.name='文老师' and b.name='黑龙江' 
return p
---------------------
MATCH (a:Teacher), (b:Course)
WHERE a.name = '文老师' AND b.name = '大数据存储与管理技术'
CREATE (a)-[r:讲授]->(b)

match (p)-[r]-(b) where p.name='文老师' //查询与文老师相关的所有节点及关系
return p,r,b
----------------------------
match(n) return n; //查询所有节点和边
match(n:Teacher) return n;//查询所有标签为Teacher的节点
match (p:Teacher) where p.name='文老师' return p;//条件查询
match (p:Student) where p.name in ['王五','李四'] return p;
match (t)-[r:讲授]->(c) return r

MATCH (a:Person), (b:Person)
WHERE a.name = 'A' AND b.name = 'B'
CREATE (a)-[r:RELTYPE]->(b)

5.找到数据库中出现的唯一关系类型
match ()-[r]-() return distinct type(r)
----------------------------
//更新节点的label名,属性名
有时候 发现节点的 label 名字起错了怎么修改呢?!
一个节点是可以有多个 label 的 ,它的 labels 是一个列表。
查看节点的 label 可以用  labels(n) 命令。
所以,想要修改节点的 label ,可以先新加 label ,再删除旧的的label
match (n:CAR) set n:NEW remove n:CAR 
match(n:NEW) RETURN  labels(n)

同理,可以修改节点的  property
match(n:CAR) SET n.new_property = n.old_property remove n.old_proerty
这个也是给节点加了一个新的属性,同时,把就属性的值赋给新属性。然后再把旧的属性给删掉

//给节点增加一个属性,直接赋值即可
match (p:Teacher) where p.name='文老师'
set p.birthDay='1967.09.13'

//显示所有老师的所有属性
match (p:Teacher) return properties(p)
//可见不一定要求所有Teacher都有相同的属性
{
  "birthDay": "1967.09.13",
  "name": "文老师",
  "title": "教授"
}
{
  "name": "吕老师",
  "title": "副教授"
}

//查询所有标签名------------
方法1:match (n) return distinct labels(n) as 标签名
标签名
["Student"]
["Course"]
["Teacher"]
不用disticnt 会出现重复
match (n) return labels(n) as 标签名,count(*) as 实例个数
标签名	实例个数
["Student"]	3
["Course"]	6
["Teacher"]	4

方法2:调用函数
call db.labels 

"Student"
"Course"
"Teacher"
-------------------------------------
match (p:Student) where p.name in ['王五','李四']
match (c:Course) where c.name ='大数据应用技术'
CREATE 
	(p)-[:选修]->(c)

match (p:Student) where p.name in ['张三','李四']
match (c:Course) where c.name ='大数据存储与管理技术'
CREATE 
	(p)-[:选修]->(c)	

match (p:Student)-[r:选修]->(c:Course) return p,r,c //查询所有学生选课情况
match (p)-[r:选修]->(c)  return p,r,c //展示所有学生选课情况
match (t:Teacher)-[r:讲授]->(c:Course) return t,r,c //展示所有讲授课程情况
match (t)-[r:讲授]->(c) return t,r,c	//展示所有讲授课程情况

match (p:Student) where p.name in ['张三','李四']
return (p)-[:选修]->()

match (p)-[r:选修]->(c)
match (t)-[r:讲授]->(c) 
create (p)-[:听课]->(t) //需要先创建一个听课的关系后才能查询

match (p)-[:选修]->(c)<-[:讲授]-(t) return p,(p)-[:听课]->(t),t //试图返回一个没有预告定义的关系,但没有结果

 
5.找到数据库中出现的唯一关系类型
match ()-[r]-() return distinct type(r)

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值