neo4j - Neo4j Cypher - 如何限制 50% 的 MATCH 结果
问题描述
我想限制 50% 的结果,MATCH
但它看起来LIMIT
不接受动态值。
我试过:
MATCH (:Profile)
WITH COUNT(*) AS c
MATCH (n:Profile)
WITH n ORDER BY rand() LIMIT toInt(c * 0.5)
RETURN n
然后我得到了错误:
It is not allowed to refer to variables in LIMIT
那么有没有办法在不使用 2 个单独的查询的情况下做到这一点?
解决方案
这就是我的看法。
- 获取所有配置文件并为每一行创建一个随机数
- 将所有配置文件收集到配置文件列表中并按随机数 (r) 排序
- 计算配置文件列表大小的 50%
- 展开列表从 start 到 cnt 然后返回每个节点
MATCH (n:Profile)
WITH n, rand() as r ORDER by r
WITH collect(n) as profile_lst
WITH profile_lst, toInt(size(profile_lst)/2) as cnt
UNWIND profile_lst[0..cnt] as prof
RETURN prof
推荐阅读
- node.js - 如何在 Angular 9 上实现 MongoDB?
- javascript - Firefox 报告我的 KEY 已死!但它并没有告诉我哪个
- bash - bash 来查看多个目录的文件数
- java - 使用一个“sendPost”功能发送多个请求
- sas - SAS 字符比较
- windows - 如何使用 CMD 和 reg.exe 添加具有包含空格和参数的长路径的程序可执行文件?
- firebase - 如何在日期为 datenow 的 firebase 选择条件中查询
- c# - 在使用 MVVM 单击 ListView 项目内的按钮时选择 ListView 中的项目
- scrapy - 如何使用 Scrapyd 和 ScrapydWeb 在集群中分布蜘蛛?
- ios - 在 IOS 13 中,当 ViewController 未推送时,Google Map 相机位置未显示准确。附上两个屏幕截图