# 1212社群
刪除點和關係
```
MATCH (n)-[r]-()
DELETE n,r
```
vc67h-x09us.csv
```
match (n:mynode)
return n, count(n)
```
匯入資料
```
//LOAD terrorist_data_subset
USING PERIODIC COMMIT 1000
LOAD CSV WITH HEADERS FROM "file:///terrorist_data_subset.csv" AS row
MERGE (c:Country {Name:row.Country})
MERGE (a:Actor {Name: row.ActorName, Aliases: row.Aliases, Type: row.ActorType})
MERGE (o:Organization {Name: row.AffiliationTo})
MERGE (a)-[:AFFILIATED_TO {Start: row.AffiliationStartDate, End: row.AffiliationEndDate}]->(o)
MERGE(c)<-[:IS_FROM]-(a);
```
### root 就是跟節點,我可以指向別人但是別人沒有指向我
開始找節點:
where是條件
```
match (m)-[r:TO]->(n:MyNode)
where not(()-->(m))
# -->一步(沒有人直接指向我)
return m
```
```
match(n)
where n.Name = 'Afghanistan'
return labels(n)
#labels就是屬性
```
## 找到王菲和謝建立新關係
1.先找到點王菲
```
match (n:`明星`{`姓名`:'王菲'})
//告訴屬性為明星
return n
```
```
match (n:`明星`{`姓名`:'王菲'})-[r]->(m:`明星`{`姓名`:'謝霆鋒'})
merge(n)-[:朋友]->(m)
merge(n)<-[:朋友]-(m)
return n,m
```
3.自己討厭自己 自己肯定自己
```
match (n:`明星`{`姓名`:'王菲'})
//-[r]->(n:`明星`{`姓名`:'王菲'})
merge(n)-[:肯定]->(n)
merge(n)<-[:討厭]-(n)
return n
```

在可能幾千個node之中只要觀看名字為'A','B','C','D','E'的
```
match(n)-[r:TO]-(m)
where n.Name in ['A','B','C','D','E'] and m.Name in ['A','B','C','D','E']
return n ,r, m
```

## 針對特定節點找出長度
### h到p最短路徑
方法1:
return p order by length(p) asc limit 1
```
match p = (a)-[:TO*]-(c)
where a.Name='H' and c.Name = 'P'
return p order by length(p) asc limit 1
//asc遞增排序 limit=1 所以是找最小路徑
```

方法2:shortestPath
```
match p =shortestPath((a)-[:TO*]-(c))
where a.Name='H' and c.Name = 'P'
return p ,length(p) limit 1
//asc遞增排序 limit1 所以找最小路徑
```
```
match p =shortestPath((a)-[:TO*]-(c))
where a.Name='H' and c.Name = 'P'
return extract(n in NODES(p)|n.Name) as path
//nodes(p)那邊就是function
//as path回傳一定是path就是回傳時包裝給你成path
//asc遞增排序 limit1 所以找最小路徑
```


```
match p =shortestPath((a)-[:TO*]-(c))
where a.Name='H' and c.Name = 'P'
and length(nodes(p))>5
return extract(n in nodes(p)|n.Name)as paths , length(p)
//把東西unpacked出來
//and 可以多新增一個條件
```

找尋直徑
```
match(n:MyNode),(m:MyNode)
where n<>m //n不可以為m的意思
with n,m
match p = shortestPath((n)-[*]->(m))
return n.Name, m.Name,length(p) order by length(p) desc limit 1
//遞減
//with 巢狀查詢 先上面查詢 後在找下面
```
最長

最短

```
match p =shortestPath((a)-[:TO*]-(c))
where a.Name='H' and c.Name = 'P'
return
extract(n in nodes(p)|n.Name) as nodes,
length(p) as pathLength,
reduce(s=0, e in relationships(p)|s+toInt(e.dist))as pathDist
//s=0叫做變數宣告,先說之後要做一個事情前需要先宣告要利用什麼來做
//toInt轉換成 int
//reduce縮減的感覺 在這裏通常都是先做一個什麼運算之後回傳一個
```
```
match p =(a)-[:TO*]-(c)
where a.Name='H' and c.Name = 'P'
return
extract(n in nodes(p)|n.Name) as nodes,
length(p) as pathLength,
reduce(s=0, e in relationships(p)|s+toInt(e.dist))as pathDist limit 1
//這邊最後會是一個總值
//s=0叫做變數宣告,先說之後要做一個事情前需要先宣告要利用什麼來做
//toInt轉換成 int
//reduce縮減的感覺 在這裏通常都是先做一個什麼運算之後回傳一個
```