Neo4j Cypher语法整理

  • 查询John的朋友的朋友

添加5个人名节点,名字分别为

1CREATE (:People{name:"John"}) 2CREATE (:People{name:"Joe"}) 3CREATE (:People{name:"Sara"}) 4CREATE (:People{name:"Steve"}) 5CREATE (:People{name:"Maria"})

此时查询所有人的结果如下

match (people:People) return people

给这5个人添加上朋友关系

1MATCH (a:People{name:"John"}) MATCH (b:People{name:"Joe"}) CREATE (a) -[:friend]->(b) 2MATCH (a:People{name:"John"}) MATCH (b:People{name:"Sara"}) CREATE (a) -[:friend]->(b) 3MATCH (a:People{name:"Joe"}) MATCH (b:People{name:"Steve"}) CREATE (a) -[:friend]->(b) 4MATCH (a:People{name:"Sara"}) MATCH (b:People{name:"Maria"}) CREATE (a) -[:friend]->(b)

再次查询

match (people:People) return people

查询John朋友的朋友的名字

match (john {name:'John'})-[:friend]->()-[:friend]->(fof) return john.name,fof.name

结果

因为这里查询的只是名字,而不是节点,所以只会显示各自的名称。

  • 给定一个用户名列表,找到名字在列表中的所有节点。匹配他们的朋友,仅返回那些他们关注的name属性以'S'开头的用户

    match (user)-[:friend]->(follower) where user.name in ['Joe','John','Sara','Maria','Steve'] and follower.name =~'S.*' return user.name,follower.name

结果

  • 获取John和他的朋友数量

    match (n {name:'John'})-[:friend]->(friend) with n,count(friend) as friendsCount return n,friendsCount

  • 获取John的朋友数量并保存为John的一个属性

    match (n {name:'John'})-[:friend]->(friend) with n,count(friend) as friendsCount set n.friendsCount=friendsCount return n.friendsCount

  • 查询朋友的朋友,不包含自己

添加3个用户,两个好友关系

1create (adam:User {name:'Adam'}),(pernilla:User {name:'Pernilla'}), 2(david:User {name:'David'}),(adam)-[:friend]->(pernilla), 3(pernilla)-[:friend]->(david)

查询结果

match (n:User) return n

match (user {name:'Adam'})-[:friend]->()-[:friend]->(friend) return friend

match (user {name:'Adam'})-[:friend]->(friend),(friend)-[:friend]-(alluser) return alluser

 该方式也是不包含自己的,可以与下面包含自己的做出比较。

  • 查询朋友的朋友,包含自己

    match (user {name:'Adam'})-[:friend]->(friend) match (friend)-[:friend]-(alluser) return alluser
    

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java将前端的json数组字符串转换为列表

记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang

KVM调整cpu和内存

一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid

JS 对象数组Array 根据对象object key的值排序sort,很风骚哦

有个js对象数组varary\{id:1,name:"b"},{id:2,name:"b"}\需求是根据name或者id的值来排序,这里有个风骚的函数函数定义:function keysrt(key,desc) {  return function(a,b){    return desc ? ~~(ak