为了演示条件 先搞几个节点
CREATE (stu:Student{id:1,name:"Jim",age:18,grade:2})
CREATE (stu:Student{id:2,name:"Tom",age:19,grade:3})
CREATE (stu:Student{id:3,name:"Lucy",age:17,grade:1})
CREATE (stu:Student{id:4,name:"LiLi",age:17})
CREATE (stu:Student{id:5,name:"James",age:19,grade:3,email:'james@java-er.com'})
MATCH (stu:Student) where stu.name='Jim' return stu
MATCH (stu:Student) where stu.name='Jim' or stu.name='Lucy' return stu
除了OR还有 AND XOR NOT
MATCH (stu:Student) where stu.age>=18 return stu
MATCH (a:Student) WHERE a.name IN ['Jim', 'Tom'] RETURN a.name,a.age
范围查询
MATCH (a:Student) WHERE a.age>18 and a.age<20 return a
MATCH (stu:Student) where stu.grade is not null return stu
MATCH (stu:Student) WITH stu.name as name WHERE stu.age = 18 RETURN name
匹配名字开头为L的学生
MATCH (stu:Student) WHERE stu.name STARTS WITH 'L' RETURN stu
匹配名字结尾为y的学生
MATCH (stu:Student) WHERE stu.name ENDS WITH 'y' RETURN stu
匹配名字包含i的学生
MATCH (stu:Student) WHERE stu.name CONTAINS 'i' RETURN stu
不的情况举例子,不是y结尾的学生
MATCH (stu:Student) WHERE NOT stu.name ENDS WITH 'y' RETURN stu
MATCH (stu:Student) WHERE stu.name =~ 'Ji.*' RETURN stu
正则表达式逃离
MATCH (stu:Student) WHERE stu.email =~ '.*\\.com' RETURN stu
正则表达式,不要区分大小写
MATCH (stu:Student) WHERE stu.name =~ '(?i)JA.*' RETURN stu.name, stu.age
下面打造关系
清空数据库
MATCH (n) DETACH DELETE n
批量执行
//创建一个关系,Tom Love Luna
CREATE (:Person{name:'Tom'})-[:Love]->(:Person{name:'Luna'})
//创建一个关系,Jim LIKES Lucy
CREATE (:Person{name:'Jim'})-[:LIKES]->(:Person{name:'Lucy'})
//创造一个黑狗
CREATE (:Dog{color:'black'})
以下语句,一条一条在浏览器里执行
//让Jim拥有这条黑狗
MATCH (p:Person),(dog:Dog) where p.name='Jim' and dog.color='black'
CREATE (p)-[r1:has_dog]->(dog) return r1
//查到Lucy让她拥有一条新创造的白狗
MATCH (p:Person) where p.name='Lucy'
CREATE (p)-[r2:has_dog]->(dog:Dog{color:'white'}) return r2
//给Lucy的白狗一个玩具
MATCH (dog:Dog)where dog.color='white'
CREATE (dog)-[r3:has_toy]->(toy:Toy{name:'apple'}) return r3
“查询和Lucy这个人,有关系的节点”
MATCH (lucy:Person {name: 'Lucy'}), (other:Person) WHERE (other)-->(lucy) RETURN other.name
╒════════════╕
│"other.name"│
╞════════════╡
│"Jim" │
└────────────┘
没有关系的情况NOT
MATCH (lucy:Person {name: 'Lucy'}), (other:Person) WHERE NOT (other)-->(lucy) RETURN other.name
╒════════════╕
│"other.name"│
╞════════════╡
│"Tom" │
├────────────┤
│"Luna" │
├────────────┤
│"Lucy" │
└────────────┘
MATCH (n:Person) WHERE (n)-[:LIKES]-({name: 'Jim'}) RETURN n
╒═══════════════╕
│"n" │
╞═══════════════╡
│{"name":"Lucy"}│
└───────────────┘
MATCH (n:Person) WHERE (n)-[:LIKES]->({name: 'Jim'}) RETURN n
MATCH (n:Person) WHERE (n)-[:LIKES]->({name: 'Lucy'}) RETURN n
╒══════════════╕
│"n" │
╞══════════════╡
│{"name":"Jim"}│
└──────────────┘
我们发现第一个语句查不到任何人,第二个可以查到 Jim
查询关系类型,正则匹配,所有L开头关系的都查出来,这样Like和Love都出来了
MATCH (n:Person)-[r]->()
WHERE type(r) =~ 'L.*'
RETURN type(r), r
╒═════════╤═══╕
│"type(r)"│"r"│
╞═════════╪═══╡
│"Love" │{} │
├─────────┼───┤
│"LIKES" │{} │
└─────────┴───┘
查询有狗的人
MATCH (p:Person) WHERE EXISTS { MATCH (p)-[:has_dog]->(:Dog) } RETURN p.name as name
╒══════╕
│"name"│
╞══════╡
│"Jim" │
├──────┤
│"Lucy"│
└──────┘
查询嵌套,查询有黑狗的那个人
MATCH (p:Person) WHERE EXISTS { MATCH (p)-[:has_dog]->(dog:Dog) WHERE dog.color = 'black' } RETURN p.name AS name
╒══════╕
│"name"│
╞══════╡
│"Jim" │
└──────┘
查询有狗,且他的狗有玩具的那个人
MATCH (p:Person) WHERE EXISTS { MATCH (p)-[:has_dog]->(dog:Dog) WHERE EXISTS { MATCH (dog)-[:has_toy]->(toy:Toy) WHERE toy.name = 'apple' } } RETURN p.name AS name
╒══════╕
│"name"│
╞══════╡
│"Lucy"│
└──────┘
将where条件,写在MATCH内的查询模式
MATCH (p:Person) WHERE EXISTS { MATCH (p)-[:has_dog]->(dog:Dog) WHERE EXISTS { MATCH (dog)-[:has_toy]->(toy:Toy{name:'apple'}) }} RETURN p.name AS name
╒══════╕
│"name"│
╞══════╡
│"Lucy"│
└──────┘
MATCH (a:Person {name: 'Jim'}) RETURN [(a)-->(b WHERE b:Person) | b.name] AS friends
╒═════════╕
│"friends"│
╞═════════╡
│["Lucy"] │
└─────────┘