如何限制密码中的子查询? [英] How to limit a subquery in cypher?

查看:66
本文介绍了如何限制密码中的子查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的图形数据库中有 3 个事物,并且这 3 个事物中的每一个都附加了 0 个或多个子事物.如何进行查询以检索图中每个事物的前 2 个子事物(根据某种任意排序).

Say I have 3 things in my graph database and each of these 3 things have 0 or more subthings attached to them. How do I make a query to retrieve the top 2 subthings for each of the things in the graph (according to some arbitrary ordering).

这是一个示例设置:

CREATE (t1:Thing)-[:X]->(a1:SubThing),
             (t1)-[:X]->(a2:SubThing),
             (t1)-[:X]->(a3:SubThing),
             (t1)-[:X]->(a4:SubThing),
       (t2:Thing)-[:X]->(b1:SubThing),
             (t2)-[:X]->(b2:SubThing),
             (t2)-[:X]->(b3:SubThing),
       (t3:Thing);

我可以运行什么匹配命令来接收这样的表格:

What match command could I run to receive a table like this:

t           s
(0:Thing)   (3:SubThing)
(0:Thing)   (4:SubThing)
(1:Thing)   (7:SubThing)
(1:Thing)   (8:SubThing)
(2:Thing)   null

这是我用来解决它的 console.neo4j.org.

Here's a console.neo4j.org I was using to work it out.

http://console.neo4j.org/r/k32uyy

推荐答案

这可能是最好的方法.我无法展开集合,否则我将丢失空的 :Thing.

This is probably the best way to do it. I can't unwind the collection or else I'll lose the empty :Thing.

MATCH (t:Thing)
OPTIONAL MATCH (t)-->(s:SubThing)
WITH t, s
ORDER BY id(s)
RETURN t, collect(s)[0..2]
ORDER BY id(t)

它返回这个:

t           collect(s)[0..2]
(0:Thing)   [(3:SubThing), (4:SubThing)]
(1:Thing)   [(7:SubThing), (8:SubThing)]
(2:Thing)   []

这篇关于如何限制密码中的子查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆