Neo4j - CQL General Clauses

1. Return Clause

1.1 Returning Single Node

Create (node:label {properties}) 
RETURN node

1.2 Returning Multiple Nodes

CREATE (Ind:Country {name: "India", result: "Winners"}) 
CREATE (CT2013:Tornament {name: "ICC Champions Trophy 2013"}) 
RETURN Ind, CT2013 

1.3 Returning Relationships

CREATE (node1)-[Relationship:Relationship_type]->(node2) 
RETURN Relationship 

eg.

CREATE (Ind)-[r1:WINNERS_OF {NRR:0.938 ,pts:6}]->(CT2013) 
CREATE(Dhoni)-[r2:CAPTAIN_OF]->(Ind) 
RETURN r1, r2 
Return Relationship

1.4 Returning Properties

Match (node:label {properties . . . . . . . . . . }) 
Return node.property 

eg.

Match (Dhoni:player {name: "MahendraSingh Dhoni", YOB: 1981, POB: "Ranchi"}) 
Return Dhoni.name, Dhoni.POB 
Return Property

1.5 Returning All Elements

Match p = (n {name: "India", result: "Winners"})-[r]-(x)  
RETURN * 

1.6 Returning a Variable With a Column Alias

You can return a particular column with alias using RETURN clause in Neo4j.

Match (Dhoni:player {name: "MahendraSingh Dhoni", YOB: 1981, POB: "Ranchi"}) 
Return Dhoni.POB as Place Of Birth
Alias

2. Order By Clause

2.1 Simple Order By

MATCH (n)  
RETURN n.property1, n.property2 . . . . . . . .  
ORDER BY n.property

eg.

CREATE(Dhawan:player{name:"shikar Dhawan", YOB: 1985, runs:363, country: "India"})
CREATE(Jonathan:player{name:"Jonathan Trott", YOB:1981, runs:229, country:"South Africa"})
CREATE(Sangakkara:player{name:"Kumar Sangakkara", YOB:1977, runs:222, country:"Srilanka"})
CREATE(Rohit:player{name:"Rohit Sharma", YOB: 1987, runs:177, country:"India"})
CREATE(Virat:player{name:"Virat Kohli", YOB: 1988, runs:176, country:"India"})
MATCH (n)  
RETURN n.name, n.runs 
ORDER BY n.runs 
Order by Result

2.2 Ordering Nodes by Multiple Properties

MATCH (n) 
RETURN n 
ORDER BY n.age, n.name 

eg.

MATCH (n) 
RETURN n.name, n.runs, n.country 
ORDER BY n.runs, n.country
image.png

2.3 Ordering Nodes by Descending Order

MATCH (n) 
RETURN n 
ORDER BY n.name DESC 
MATCH (n)  
RETURN n.name, n.runs 
ORDER BY n.runs DESC 

3. Limit Clause

The limit clause is used to limit the number of rows in the output.

3.1

MATCH (n) 
RETURN n 
ORDER BY n.name 
LIMIT 3 

eg.

MATCH (n)  
RETURN n.name, n.runs 
ORDER BY n.runs DESC 
LIMIT 3 

3.2 Limit with expression

MATCH (n) 
RETURN n.name, n.runs 
ORDER BY n.runs DESC 
LIMIT toInt(3 * rand())+ 1 

4. Skip

4.1 Skip

Returns all the nodes in the database skipping the first 3 nodes.

MATCH (n)  
RETURN n.name, n.runs 
ORDER BY n.runs DESC 
SKIP 3 

4.2 Skip Using Expression

You can skip the records of a result using an expression.

MATCH (n)  
RETURN n.name, n.runs 
ORDER BY n.runs DESC 
SKIP toInt (2*rand())+ 1 

5. With Clause

You can chain the query arts together using the WITH clause.

MATCH (n) 
WITH n 
ORDER BY n.property 
RETURN collect(n.property) 

eg.

MATCH (n) 
WITH n 
ORDER BY n.name DESC LIMIT 3 
RETURN collect(n.name) 
With

6. Unwind Clause

Following is a sample Cypher Query which unwinds a list. --拆解一個(gè)collection

UNWIND ['a', 'b', 'c', 'd'] AS x 
RETURN x 
image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 第一次與簡書做朋友,感覺很不錯(cuò)。給自己定了一個(gè)標(biāo)簽,讓簡書成為自己的寫照。每天睡覺前25分鐘,給自己定了一...
    欽欽閱讀 262評(píng)論 0 1
  • 大道理不是沒用,只是光聽沒用,道理走心并融入行動(dòng),養(yǎng)成習(xí)慣踐行道理,人生才會(huì)改變。紙上得來終覺淺,絕知此事...
    名字什么的太麻煩閱讀 338評(píng)論 0 0
  • 大半夜的,此時(shí)此刻,你想起的那個(gè)人就是你現(xiàn)在最愛的人吧。 一直在想:他喜不喜歡我?他為什么不回復(fù)我信息?他是不是只...
    甜覓覓閱讀 1,989評(píng)論 4 16
  • 福爾摩斯基本上已經(jīng)成為了人們對于“偵探”(當(dāng)然更準(zhǔn)確地說是“咨詢偵探”)的第一映像了。如果要按中國人的傳統(tǒng)立一個(gè)祖...
    LostAbaddon閱讀 10,963評(píng)論 10 17

友情鏈接更多精彩內(nèi)容