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. --拆解一个collection

UNWIND ['a', 'b', 'c', 'd'] AS x 
RETURN x 
image.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 第一次与简书做朋友,感觉很不错。给自己定了一个标签,让简书成为自己的写照。每天睡觉前25分钟,给自己定了一...
    钦钦阅读 204评论 0 1
  • 大道理不是没用,只是光听没用,道理走心并融入行动,养成习惯践行道理,人生才会改变。纸上得来终觉浅,绝知此事...
    名字什么的太麻烦阅读 291评论 0 0
  • 大半夜的,此时此刻,你想起的那个人就是你现在最爱的人吧。 一直在想:他喜不喜欢我?他为什么不回复我信息?他是不是只...
    甜觅觅阅读 1,926评论 4 16
  • 福尔摩斯基本上已经成为了人们对于“侦探”(当然更准确地说是“咨询侦探”)的第一映像了。如果要按中国人的传统立一个祖...
    LostAbaddon阅读 10,341评论 10 17