```sql
match p = (n:dwt_ent_company{company_name:'浙江投融界科技有限公司'})-[]-() return p
MATCH (n) WHERE EXISTS(n.company_id) RETURN DISTINCT "node" as entity, n.company_id AS company_id LIMIT 25
UNION ALL
MATCH ()-[r]-() WHERE EXISTS(r.company_id) RETURN DISTINCT "relationship" AS entity, r.company_id AS company_id LIMIT 25
MATCH p=()-[r:dwt_rel_invest]->() RETURN p LIMIT 25
match p=()-[rel:dwt_rel_invested]->() return p limit 20
match p=(n:dwt_ent_company) return p limit 20
match p=(n:dwt_ent_company)-[]->() where n.industry='房屋建筑业' return p limit 20
match p=(n:dwt_ent_company{company_name:'浙江投融界科技有限公司'})-[r]->(dwt_ent_company) return type(r)
match p=(n:dwt_ent_company)-[r]->(dwt_ent_company) return count(p);
match p=(n:dwt_ent_company) return count(p);
match p=(n:dwt_ent_person) return count(p);
match p=(n:dwt_ent_person)-[r]-() return count(p);
match p=(n:dwt_ent_person)-[]-() return count(p);#上下两条语句等价,r加不加不影响。r只是相当于关系的变量字段
match p=(n:dwt_ent_person)-[r:dwt_rel_invested]-() where n.rel_ratio<>'-' RETURN p limit 10;
#查询一家公司对外投资的占比不为‘-’的数量?
match (n) where n.company_name='科大讯飞股份有限公司' and n.rel_ratio<>'-' RETURN count(n);
match (n:dwt_ent_company{company_name:'科大讯飞股份有限公司'})-[]-() RETURN count(n);
match (n:dwt_ent_company{company_name:'科大讯飞股份有限公司'})-[r:dwt_rel_invest]-() RETURN count(n);
match (n:dwt_ent_company{company_name:'科大讯飞股份有限公司'})-[r:dwt_rel_invested]-() RETURN count(n);
match (n:dwt_ent_company{company_name:'科大讯飞股份有限公司'})-[r:dwt_rel_occupy]-() RETURN count(n);
#注意查询的方向性问题
MATCH p=(n:company{company_name:'原子高科股份有限公司'})<-[r:`下游客户`]-() RETURN p
#最短路径算法
match(n1:company{company_name:'河北华美光电子有限公司'}),(n2:company{company_name:'北京华环电子股份有限公司'}),p=shortestpath((n1)-[*..5]->(n2)) return p
#所有最短路径算法(返回路径深度内的所有路径)
match(n1:company{company_name:'河北华美光电子有限公司'}),(n2:company{company_name:'北京华环电子股份有限公司'}),p=allshortestpath((n1)-[*..5]->(n2)) return p
#---------------------------------------------------------------------------------------
#7800W+企业
#企业数
match (n:nds_qyxx) return count(n) #78682220
MATCH p=(n:nds_qyxx) RETURN count(p) #速度比上面的语句慢15000倍
#人员数
MATCH (n:nds_ryxx) RETURN count(n) #107896154
MATCH (n:nds_ryxx) RETURN count(n) as cnt
#关系数统计
MATCH (n:nds_qyxx)-[r:rel_dwtz]-() RETURN count(n) as cnt #3276065
MATCH (n:nds_qyxx)-[r:rel_gdxx]-() RETURN count(n) as cnt #19995245
MATCH (n:nds_qyxx)-[r:rel_rzgx]-() RETURN count(n) as cnt #109098909
#查看索引
:schema
#结果:
Indexes
ON :nds_qyxx(company_id) ONLINE
ON :nds_qyxx(company_name) ONLINE
ON :nds_ryxx(ryid) ONLINE
ON :nds_ryxx(rymc) ONLINE
ON :nds_ryxx(xm) ONLINE
No constraints
#where正则表达式用法
1、starts with
2、ends with
3、contains
4、=~ #语法为 : =~ "regexp"。 如返回名字以a开头的人:match (p:Person) where p.name =~ 'a.*'
MATCH (n:nds_ryxx) where n.rymc contains '公司' RETURN count(n)
MATCH (n) WHERE NOT n.name ENDS WITH 'y' RETURN n.name
match (n:nds_qyxx) where n.company_name =~'(待清理).*' return n limit 10
MATCH (n:ent_product) where n.proname =~'半导体.*' return n limit 50
#查询国有性质的企业及其股东、一级投资企业获取
#匹配路径*1..5,*..5
使用深度运算符
当实现多深度关系节点查询时,显然使用以上方式比较繁琐。
可变数量的关系->节点可以使用-[:TYPE*minHops..maxHops]->
match (n:nds_qyxx)-[r:rel_gdxx*1..5]-() where n.company_name ends with '公司' and (n.company_type contains '国有控股' or n.company_type contains '国有独资') return count(n)
match (n:nds_qyxx)-[r:rel_gdxx*1..3]-(n1:nds_qyxx) where n.company_name ends with '公司' and (n.company_type contains '国有控股' or n.company_type contains '国有独资') return n,n1
match (n:nds_qyxx)-[r:rel_gdxx*1..5]-(m:nds_qyxx) where n.company_name ends with '公司' and (n.company_type contains '国有控股' or n.company_type contains '国有独资') return count(n)
match p=(n:nds_qyxx)-[r:rel_gdxx*1..5]-(m:nds_qyxx) where n.company_name ends with '公司' and (n.company_type contains '国有控股' or n.company_type contains '国有独资') return count(p)
match (n:nds_qyxx) where n.company_name ends with '公司' and (n.company_type contains '国有控股' or n.company_type contains '国有独资') return count(n) #99923
match p=(n:nds_qyxx)-[r:rel_gdxx]-(m:nds_qyxx) where n.company_name ends with '公司' and m.company_name ends with '公司'
and (n.company_type contains '国有控股' or n.company_type contains '国有独资') return p
match p=(n:nds_qyxx)-[r:rel_dwtz]-(m:nds_qyxx) where n.company_name ends with '公司' and m.company_name ends with '公司' and (n.company_type contains '国有控股' or n.company_type contains '国有独资') return p
match p=(n:nds_qyxx)-[r:rel_gdxx*1..3]-(n1:nds_qyxx) where n.company_name ends with '公司' and (n.company_type contains '国有控股' or n.company_type contains '国有独资') return nodes(p)
#创建索引
CREATE INDEX ON :nds_qyxx(company_type) #索引建立后只是Populating状态,需要注意重启数据库,并关闭管理网页。
#查询neo4j所有节点的属性名
采用keys()查询:
MATCH (n:Test) RETURN distinct keys(n)
MATCH (n:nds_qyxx) RETURN distinct keys(n)
#统计节点上的关系数目
MATCH (n:Person {name:‘Keanu Reeves’})-[]-() RETURN count(*);
#detach delete 删除节点和关系
MATCH p=()-[r:`ent[0][4]`]->() detach delete p
#分组汇总
match (n:nds_qyxx{company_name:'浙江投融界科技有限公司'})-[r:rel_dwtz]-(qy) return n.company_name,count(qy) as cnt;
match (n:nds_qyxx{company_name:'浙江投融界科技有限公司'})-[r:rel_dwtz]-(qy)
WITH qy, count(qy) AS foaf
WHERE foaf > 2
RETURN qy
```sqlmatch p = (n:dwt_ent_company{company_name:'浙江投融界科技有限公司'})-[]-() return pMATCH (n) WHERE EXISTS(n.company_id) RETURN DISTINCT "node" as entity, n.company_id AS company_id LIMIT 25 UNION ALL MATCH ()-[r]-() WHERE EXISTS(r.company_id) RETURN DISTI.