neo4j查詢語句


一:查詢

比較操作:

=
<>
<
>
<=
>=

 

布爾操作:

AND
OR
NOT
XOR

 

 

1、把節點的前兩個字為"提示"的節點去除"提示":

match(l) where l.name=~'提示.*' 
with collect(l.name)
as result 
unwind result as row 
return substring(row,2)

2、把帶提示的節點,更新為不帶提示:

match(l) where l.name=~'提示.*' 
with collect(l.name)
as result 
unwind result as row 
match(h) where h.name=row set h.name=substring(row,2)
return h

 3、分組查詢,每個標簽的數目,按名字的數目倒排

match(l) 
with collect(l.name) as collectName
unwind collectName as p
return p,count(*)as num order by num desc

 4.查詢不存在emergency屬性的疾病

match(d:Disease) where not exists (d.emergency) return d.name

 5.查詢Condition標簽中包含"任二"的節點

match(c:Condition) where c.name contains "任二" return c.name

6.查詢疾病沒有high_risk屬性的節點

match(d:Disease) where d.high_risk is NULL return d.name

 7.更新標簽名

MATCH (n:User:Teacher) REMOVE n:Student  RETURN n

8.更新關系名

MATCH (n:User {name:"foo"})-[r:REL]->(m:User {name:"bar"})
CREATE (n)-[r2:NEWREL]->(m)
SET r2 = r
WITH r
DELETE r

 9.其他

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)-[ *35]->(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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM