原文鏈接:https://blog.csdn.net/weixin_40771521/article/details/95936491
1.如何找到一個(gè)節(jié)點(diǎn)x钧萍,x以某種關(guān)系同時(shí)連接兩個(gè)不同節(jié)點(diǎn)a和b
match (a)-[r:relation]->(x)<-[r:relation]-(b) return x
2.如何找到節(jié)點(diǎn)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.如何找到節(jié)點(diǎn)a和b之間以某種關(guān)系相連接的最短路徑
p=shortestpath((a)-[r:relationname]->(b)) return nodes(p)
4.找到數(shù)據(jù)庫(kù)中出現(xiàn)的唯一節(jié)點(diǎn)標(biāo)簽
match n return distinct labels(n)
5.找到數(shù)據(jù)庫(kù)中出現(xiàn)的唯一關(guān)系類型
match n-[r]-() return distinct type(r)
6.找到數(shù)據(jù)庫(kù)中的唯一節(jié)點(diǎn)標(biāo)簽和唯一關(guān)系類型
match n-[r]-() return distinct labels(n),type(r)
7.找到不與任何關(guān)系(或某種關(guān)系)向連的節(jié)點(diǎn)
start n = node() match n-[r:relationname]-() where r is null return n
8.找到某個(gè)帶有特定屬性的節(jié)點(diǎn)
start n=node() match n where has (n.someproperty) return n
9.找到與某種關(guān)系相連接的全部節(jié)點(diǎn)
start n= node() match n-[r:relationshipname]-() return distinct n
10.找到節(jié)點(diǎn)和它們的關(guān)系數(shù)朵栖,并以關(guān)系數(shù)目降序排列顯示
start n=node() match n-[r]-() return n,count(r) as rel_count order by rel_count desc
11.返回圖中所有節(jié)點(diǎn)的個(gè)數(shù)
start n = node() match n return count(n)
12.(1)刪除圖中關(guān)系:start n=node() match n-[r]-() delete r
(2)刪除圖中節(jié)點(diǎn):start n =node() match n delete n
(3)刪除圖中所有東西:match (n) detach delete n
13.查詢某類節(jié)點(diǎn)下某屬性為特定值的節(jié)點(diǎn)
match (n:person)where n.name=”alice” return n
14.with
Cypher中的With關(guān)鍵字可以將前步查詢的結(jié)果作為后一步查詢的條件乓序,這個(gè)在我的工作中可是幫了大忙哈哈噪漾。下面是兩個(gè)栗子。
(1)match(p:node_se)-[re:推理?xiàng)l件]->(q:node_se) where p.name=‘FEV1%pred’and p.value=’<30%’ WITH p,re,q match (q:node_se) <-[re2:推理?xiàng)l件]- (c:node_se)return p, re,q,re2,c
(2)match(p:node_patient)-[re:個(gè)人情況]->(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:方案細(xì)節(jié)]->(d:drugs) return p, re,q,re2,c,re3,d
15.查詢符合條件的某個(gè)節(jié)點(diǎn)的id
match(p) where p.name = ‘’ and p.value = ‘’ return id(p)
16.直接連接關(guān)系節(jié)點(diǎn)進(jìn)行多層查詢
match(na:bank{id:‘001’})-[re1]->(nb:company)-[re2]->(nc:people) return na,re1,nb,re2,nc
17.可以將查詢結(jié)果賦給變量雾叭,然后返回
match data=(na:bank{id:‘001’})-[re1]->(nb:company)-[re2]->(nc:company) return data
18.變長(zhǎng)路徑檢索
變長(zhǎng)路徑的表示方式是:[*N…M]料身,N和M表示路徑長(zhǎng)度的最小值和最大值。
(a)-[ *2]->(b):表示路徑長(zhǎng)度為2胰锌,起始節(jié)點(diǎn)是a骗绕,終止節(jié)點(diǎn)是b;
(a)-[ *3…5]->(b):表示路徑長(zhǎng)度的最小值是3资昧,最大值是5酬土,起始節(jié)點(diǎn)是a,終止節(jié)點(diǎn)是b格带;
(a)-[ *…5]->(b):表示路徑長(zhǎng)度的最大值是5撤缴,起始節(jié)點(diǎn)是a,終止節(jié)點(diǎn)是b叽唱;
(a)-[ *3…]->(b):表示路徑長(zhǎng)度的最小值是3屈呕,起始節(jié)點(diǎn)是a,終止節(jié)點(diǎn)是b尔觉;
(a)-[ *]->(b):表示不限制路徑長(zhǎng)度凉袱,起始節(jié)點(diǎn)是a芥吟,終止節(jié)點(diǎn)是b侦铜;
19.Cypher對(duì)查詢的結(jié)果進(jìn)行去重
栗:match(p:node_se)-[re]->(q)where re.name <> ‘a(chǎn)nd’ return distinct(re.name)
(注:栗子中的<>為Cypher中的操作符之一专甩,表示‘不等于’)
20.更新節(jié)點(diǎn)的 labels
Neo4j中的一個(gè)節(jié)點(diǎn)可以有多個(gè) label,返回所有節(jié)點(diǎn)的label:match (n) return labels(n)
修改節(jié)點(diǎn)的 label钉稍,可以先新加 label涤躲,再刪除舊的label
match (n:label_old) set n:label_new remove n:label_old
match(n:label_new) return labels(n)
21.更新節(jié)點(diǎn)的屬性
match(n:) set n.new_property = n.old_property remove n.old_proerty