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

你可能感兴趣的:(Neo4j - CQL General Clauses)