Neo4j密码查询语言-布尔表达式的操作顺序 [英] Neo4j cypher query language - order of operations for boolean expressions

查看:59
本文介绍了Neo4j密码查询语言-布尔表达式的操作顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写查询以从Neo4J数据库中提取数据.可以说,有五个条件确定是否要从数据库中提取_____:A,B,C,D和E.确定该值的布尔表达式为:

I am trying to write a query to pull data from my Neo4J database. Lets say there are five conditions that determine whether or not I want to pull _____ from my database: A, B, C, D, and E. The boolean expression which determines this is:

A && B && (C || D || E)

从网上搜索时,我找不到任何有关Neo4J AND和OR查询遵守的操作顺序的信息(AND通常在OR之前),但是从我的观察来看,它们似乎是顺序执行的.由于我不知道要使用括号显式定义顺序,因此如何实现Cypher查询来满足上面的布尔表达式?

From scouring the web, I cannot find any information about an order of operations that Neo4J AND and OR queries abide by (AND usually precedes OR), but from my observations they seem like they execute sequentially. Since there is no way that I know of to explicitly define order, aka using parenthesis, how would one implement a Cypher query to satisfy the boolean expression above?

推荐答案

我不知道要使用括号来明确定义顺序

there is no way that I know of to explicitly define order, aka using parenthesis

不确定. ;)

首先,总是存在一个 关联或分组,即使它们是按顺序执行的,即使是隐式的也是如此.让我们看一看这很重要的情况.

First of all, there's always an association or grouping, even if it is implicit in case they are executed sequentially. Let's look at a case where this matters.

MATCH (n) RETURN CASE WHEN false AND true OR true THEN 'and' ELSE 'or' END

(只要您的数据库中至少有一个节点,这将起作用.)

(This will work as long as there is at least one node in your database.)

必须等同于((false and true) or true)(false and (true or true)),但是它们具有不同的结果.

This must be equivalent either to ((false and true) or true) or (false and (true or true)), but these have different outcomes.

(false and true) or true => false or true => true
false and (true or true) => false and true => false

上面的查询在第一种情况下将返回和",在第二种情况下将返回或",以向您显示首先执行的命令.它返回"and",因为它们是按顺序分组的.

The query above will return "and" in the first case and "or" in the second, to show you which was executed first. It returns "and", because they are grouped sequentially.

MATCH (n) RETURN CASE WHEN ((false AND true) OR true) THEN 'and' ELSE 'or' END

这还会返回和",因为这是在顺序执行布尔运算符时得到的隐式分组.

This also returns "and", because this is the implied grouping you get when you execute the boolean operators sequentially.

MATCH (n) RETURN CASE WHEN (false AND (true OR true)) THEN 'and' ELSE 'or' END

但这将返回或".

说括号确实是答案,这真是长篇大论.

That was a really long-winded way to say that adding parentheses is indeed the answer.

只是为了好玩,我试图确定优先级,看来Neo确实具有and-precedence:

Just for fun, I tried to determine precedence, and it appears that Neo does have and-precedence:

MATCH (n) RETURN CASE WHEN true or true and false THEN 'and' ELSE 'or' END

如果or或第一个分组:

If the or is grouped first:

(true or true) and false => true and false => false

如果and被首先分组:

If the and is grouped first:

true or (true and false) => true or false => true

上面的查询返回"and",表示隐式分组的构造的整体评估为真实(因此,尽管顺序出现在最后,但仍先分组).

The query above returns 'and', indicating the implicitly grouped construct evaluated to true overall (thus and was grouped first despite appearing last sequentially).

这篇关于Neo4j密码查询语言-布尔表达式的操作顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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