• Neo4j 学习笔记2


    需要造一笔数据,用来查询用:

    create (p:person{name:"宋江",sex:"",age:"38",hobby:"权力",nickName:"及时雨、呼保义、孝义黑三郎、宋公明、宋押司"});
    create (p:person{name:"宋太公",sex:"",age:"68",hobby:"",nickName:""});
    create (p:person{name:"扈三娘",sex:"",age:"24",hobby:"",nickName:"一丈青"});
    create (p:person{name:"王英",sex:"",age:"37",hobby:"",nickName:"矮脚虎"});
    create (p:person{name:"阎婆惜",sex:"",age:"22",hobby:"",nickName:""});
    create (p:person{name:"晁盖",sex:"",age:"39",hobby:"",nickName:"托塔天王"});
    create (p:person{name:"武松",sex:"",age:"33",hobby:"",nickName:"行者、武二郎"});
    create (p:person{name:"武大郎",sex:"",age:"40",hobby:"",nickName:""});
    create (p:person{name:"潘金莲",sex:"",age:"24",hobby:"",nickName:""});
    create (p:person{name:"乔郓哥",sex:"",age:"13",hobby:"",nickName:"小猴子"});
    create (p:person{name:"王婆",sex:"",age:"56",hobby:"",nickName:""});
    create (p:person{name:"西门庆",sex:"",age:"34",hobby:"",nickName:"西门大官人"});
    create (p:person{name:"玉兰",sex:"",age:"18",hobby:"",nickName:""});
    
    match (p1:person{name:"宋江"}),(p2:person{name:"宋太公"}) create (p1)-[r:父亲]->(p2) return p1,p2;
    match (p1:person{name:"宋江"}),(p2:person{name:"宋太公"}) create (p2)-[r:儿子]->(p1) return p1,p2;
    match (p1:person{name:"宋江"}),(p2:person{name:"扈三娘"}) create (p1)-[r:义妹]->(p2) return p1,p2;
    match (p1:person{name:"宋江"}),(p2:person{name:"扈三娘"}) create (p2)-[r:义兄]->(p1) return p1,p2;
    match (p1:person{name:"宋江"}),(p2:person{name:"阎婆惜"}) create (p1)-[r:小妾]->(p2) return p1,p2;
    match (p1:person{name:"宋江"}),(p2:person{name:"阎婆惜"}) create (p2)-[r:夫君]->(p1) return p1,p2;
    match (p1:person{name:"宋江"}),(p2:person{name:"王英"}) create (p1)-[r:小弟]->(p2) return p1,p2;
    match (p1:person{name:"宋江"}),(p2:person{name:"王英"}) create (p2)-[r:哥哥]->(p1) return p1,p2;
    match (p1:person{name:"宋江"}),(p2:person{name:"晁盖"}) create (p1)-[r:哥哥]->(p2) return p1,p2;
    match (p1:person{name:"宋江"}),(p2:person{name:"晁盖"}) create (p2)-[r:小弟]->(p1) return p1,p2;
    match (p1:person{name:"宋江"}),(p2:person{name:"武松"}) create (p1)-[r:小弟]->(p2) return p1,p2;
    match (p1:person{name:"宋江"}),(p2:person{name:"武松"}) create (p2)-[r:哥哥]->(p1) return p1,p2;
    match (p1:person{name:"扈三娘"}),(p2:person{name:"王英"}) merge (p1)-[r:夫君]->(p2) return p1,p2;
    match (p1:person{name:"扈三娘"}),(p2:person{name:"王英"}) merge (p2)-[r:妻子]->(p1) return p1,p2;
    match (p1:person{name:"晁盖"}),(p2:person{name:"武松"}) merge (p1)-[r:小弟]->(p2) return p1,p2;
    match (p1:person{name:"晁盖"}),(p2:person{name:"武松"}) merge (p2)-[r:哥哥]->(p1) return p1,p2;
    match (p1:person{name:"晁盖"}),(p2:person{name:"王英"}) merge (p1)-[r:小弟]->(p2) return p1,p2;
    match (p1:person{name:"晁盖"}),(p2:person{name:"王英"}) merge (p2)-[r:哥哥]->(p1) return p1,p2;
    match (p1:person{name:"武松"}),(p2:person{name:"武大郎"}) merge (p1)-[r:亲哥]->(p2) return p1,p2;
    match (p1:person{name:"武松"}),(p2:person{name:"武大郎"}) merge (p2)-[r:亲弟]->(p1) return p1,p2;
    match (p1:person{name:"武松"}),(p2:person{name:"潘金莲"}) merge (p1)-[r:嫂子]->(p2) return p1,p2;
    match (p1:person{name:"武松"}),(p2:person{name:"潘金莲"}) merge (p2)-[r:小叔子]->(p1) return p1,p2;
    match (p1:person{name:"玉兰"}),(p2:person{name:"武松"}) merge (p1)-[r:喜欢]->(p2) return p1,p2;
    match (p1:person{name:"武松"}),(p2:person{name:"西门庆"}) merge (p1)-[r:仇敌]-(p2) return p1,p2;
    match (p1:person{name:"武大郎"}),(p2:person{name:"潘金莲"}) merge (p1)-[r:妻子]->(p2) return p1,p2;
    match (p1:person{name:"武大郎"}),(p2:person{name:"潘金莲"}) merge (p2)-[r:夫君]->(p1) return p1,p2;
    match (p1:person{name:"武大郎"}),(p2:person{name:"乔郓哥"}) merge (p1)-[r:朋友]-(p2) return p1,p2;
    match (p1:person{name:"武大郎"}),(p2:person{name:"王婆"}) merge (p1)-[r:邻居]-(p2) return p1,p2;
    match (p1:person{name:"潘金莲"}),(p2:person{name:"王婆"}) merge (p1)-[r:邻居]-(p2) return p1,p2;
    match (p1:person{name:"潘金莲"}),(p2:person{name:"西门庆"}) merge (p1)-[r:情人]-(p2) return p1,p2;
    match (p1:person{name:"西门庆"}),(p2:person{name:"武大郎"}) merge (p1)-[r:杀死]->(p2) return p1,p2;
    match (p1:person{name:"西门庆"}),(p2:person{name:"王婆"}) merge (p1)-[r:认识]-(p2) return p1,p2;

    最短路径查询:宋江到王英的最短路径:

    match (p1:person{name:"宋江"}),(p2:person{name:"王英"}),p=shortestpath((p1)-[*..10]-(p2)) return p
    //p 表示路径
    //(p1)-[*..10]-(p2) 表示10层以内

    结果:

     查询所有最短路径:

    match (p1:person{name:"宋江"}),(p2:person{name:"王英"}),p=((p1)-[*1..3]->(p2)) return p

    结果:

     注意:环路还包含了自己。

    查询含有某种关系的节点数目

    match (p1:person)-[r:小弟]->() return count(p1)

  • 相关阅读:
    echarts图形的位置布局如何调整
    IDEA在写springboot项目的时候遇见的问题:Caused by: java.lang.NoClassDefFoundError: com/fasterxml/classmate/TypeResol
    【学习总结】linux命令:ps -ef | grep xxxserver | grep -v grep|wc -l
    linux中管道符“|”的作用 find的作用
    linux系统中的管道“|”命令的作用
    JDBC管理事务
    SQL注入的问题&使用PreparedStatement对象防止SQL注入
    JDBC对象详解DriverManager&Connection&Statement
    MySQL安装&使用DataGrip连接数据库
    红黑树(R-B树,对称二叉B树)
  • 原文地址:https://www.cnblogs.com/xiaozhuanfeng/p/12326058.html
Copyright © 2020-2023  润新知