对正确使用ORDER BY感到困惑

时间:2017-05-30 11:38:03

标签: neo4j neo4j-apoc

我正在试验neo4j中的日期。现在我想通过ISODateString对结果进行排序。我创建了一个像这样的密码查询:

MATCH(e:Expedition {id : "BJGYmzwZb"})-[pje]-(u:User) 
WHERE (e)-[:POSSIBLY_JOINS_EXPEDITION]-(u) OR (e)-[:JOINS_EXPEDITION]-(u) 
WITH e, u, apoc.date.parse(pje.createdAt, 's',"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") as date
ORDER by date 
OPTIONAL MATCH(u)<-[invitee:POSSIBLY_JOINS_EXPEDITION]-(e) 
OPTIONAL MATCH(u)-[attendee:JOINS_EXPEDITION]->(e)
OPTIONAL MATCH(u)-[applicant:POSSIBLY_JOINS_EXPEDITION]->(e) 
RETURN date, {user: properties(u), isInvitee: COUNT(invitee) > 0, isApplicant: COUNT(applicant) > 0, isAttendee: COUNT(attendee) > 0} as u

返回的结果未正确排序。而以下查询确实以正确的顺序返回结果。我刚刚用OPTIONAL MATCH删除了部分。

MATCH(e:Expedition {id : "BJGYmzwZb"})-[pje]-(u:User) 
WHERE (e)-[:POSSIBLY_JOINS_EXPEDITION]-(u) OR (e)-[:JOINS_EXPEDITION]-(u) 
WITH e, u, apoc.date.parse(pje.createdAt, 's',"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") as date 
ORDER by date
RETURN date, {user: properties(u)} as u

有什么建议我做错了吗?我是否需要对OPTIONAL MATCH - 添加进行不同的处理?

1 个答案:

答案 0 :(得分:1)

ORDER by date放在RETURN语句后面,如下所示:

MATCH(e:Expedition {id : "BJGYmzwZb"})-[pje]-(u:User) 
WHERE (e)-[:POSSIBLY_JOINS_EXPEDITION]-(u) OR (e)-[:JOINS_EXPEDITION]-(u) 
WITH e, u, apoc.date.parse(pje.createdAt, 's',"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") as date
OPTIONAL MATCH(u)<-[invitee:POSSIBLY_JOINS_EXPEDITION]-(e) 
OPTIONAL MATCH(u)-[attendee:JOINS_EXPEDITION]->(e)
OPTIONAL MATCH(u)-[applicant:POSSIBLY_JOINS_EXPEDITION]->(e) 
RETURN date, {user: properties(u), isInvitee: COUNT(invitee) > 0, isApplicant: COUNT(applicant) > 0, isAttendee: COUNT(attendee) > 0} as u
ORDER by date