跳转至

比较符

NebulaGraph 支持的比较符如下。

符号 说明
== 相等
!=, <> 不等于
> 大于
>= 大于等于
< 小于
<= 小于等于
IS NULL 为 NULL
IS NOT NULL 不为 NULL
IS EMPTY 不存在
IS NOT EMPTY 存在

比较操作的结果是true或者false

Note

  • 比较不同类型的值通常没有定义,结果可能是NULL或其它。
  • EMPTY当前仅用于判断,不支持函数或者运算操作,包括且不限于GROUP BYcount()sum()max()hash()collect()+*

OpenCypher 兼容性

openCypher 中没有EMPTY,因此不支持在 MATCH 语句中使用EMPTY

示例

==

字符串比较时,会区分大小写。不同类型的值不相等。

Note

nGQL 中的相等符号是==,openCypher 中的相等符号是=

nebula> RETURN 'A' == 'a', toUpper('A') == toUpper('a'), toLower('A') == toLower('a');
+------------+------------------------------+------------------------------+
| ("A"=="a") | (toUpper("A")==toUpper("a")) | (toLower("A")==toLower("a")) |
+------------+------------------------------+------------------------------+
| false      | true                         | true                         |
+------------+------------------------------+------------------------------+

nebula> RETURN '2' == 2, toInteger('2') == 2;
+----------+---------------------+
| ("2"==2) | (toInteger("2")==2) |
+----------+---------------------+
| false    | true                |
+----------+---------------------+

>

nebula> RETURN 3 > 2;
+-------+
| (3>2) |
+-------+
| true  |
+-------+

nebula> WITH 4 AS one, 3 AS two \
        RETURN one > two AS result;
+--------+
| result |
+--------+
| true   |
+--------+

>=

nebula> RETURN 2 >= "2", 2 >= 2;
+----------+--------+
| (2>="2") | (2>=2) |
+----------+--------+
| __NULL__ | true   |
+----------+--------+

<

nebula> YIELD 2.0 < 1.9;
+---------+
| (2<1.9) |
+---------+
| false   |
+---------+

<=

nebula> YIELD 0.11 <= 0.11;
+--------------+
| (0.11<=0.11) |
+--------------+
| true         |
+--------------+

!=

nebula> YIELD 1 != '1';
+----------+
| (1!="1") |
+----------+
| true     |
+----------+

IS [NOT] NULL

# 返回关于 null 的一些判断结果。
nebula> RETURN null IS NULL AS value1, null == null AS value2, null != null AS value3;
+--------+----------+----------+
| value1 | value2   | value3   |
+--------+----------+----------+
| true   | __NULL__ | __NULL__ |
+--------+----------+----------+

# 返回关于 NULL 的一些属性信息。
nebula> RETURN length(NULL), size(NULL), count(NULL), NULL IS NULL, NULL IS NOT NULL, sin(NULL), NULL + NULL, [1, NULL] IS NULL;
+--------------+------------+-------------+--------------+------------------+-----------+-------------+------------------+
| length(NULL) | size(NULL) | count(NULL) | NULL IS NULL | NULL IS NOT NULL | sin(NULL) | (NULL+NULL) | [1,NULL] IS NULL |
+--------------+------------+-------------+--------------+------------------+-----------+-------------+------------------+
| __NULL__     | __NULL__   | 0           | true         | false            | __NULL__  | __NULL__    | false            |
+--------------+------------+-------------+--------------+------------------+-----------+-------------+------------------+

# 创建 map 数据,判断 map 的 name 属性是否不为 NULL。
nebula> WITH {name: null} AS `map` \
        RETURN `map`.name IS NOT NULL;
+----------------------+
| map.name IS NOT NULL |
+----------------------+
| false                |
+----------------------+

# 创建 map1、map2、map3 的数据,并判断返回其 name 属性是否为 NULL。
nebula> WITH {name: 'Mats', name2: 'Pontus'} AS map1, \
        {name: null} AS map2, {notName: 0, notName2: null } AS map3 \
        RETURN map1.name IS NULL, map2.name IS NOT NULL, map3.name IS NULL;
+-------------------+-----------------------+-------------------+
| map1.name IS NULL | map2.name IS NOT NULL | map3.name IS NULL |
+-------------------+-----------------------+-------------------+
| false             | false                 | true              |
+-------------------+-----------------------+-------------------+

# 查询所有 Tag 为 player 的点数据,判断并返回点的 age 属性是否为 NULL、name 属性是否不为 NULL、empty 属性是否为 NULL。
nebula> MATCH (n:player) \
        RETURN n.player.age IS NULL, n.player.name IS NOT NULL, n.player.empty IS NULL;
+----------------------+---------------------------+------------------------+
| n.player.age IS NULL | n.player.name IS NOT NULL | n.player.empty IS NULL |
+----------------------+---------------------------+------------------------+
| false                | true                      | true                   |
| false                | true                      | true                   |
...

IS [NOT] EMPTY

# 判断 null 是否不存在。
nebula> RETURN null IS EMPTY;
+---------------+
| NULL IS EMPTY |
+---------------+
| false         |
+---------------+

# 判断 a 字符串是否存在。
nebula> RETURN "a" IS NOT EMPTY;
+------------------+
| "a" IS NOT EMPTY |
+------------------+
| true             |
+------------------+

# 遍历所有 player100 指向的目的点,并返回目的点数据 name 属性存在的点 id。
nebula> GO FROM "player100" OVER * WHERE properties($$).name IS NOT EMPTY YIELD dst(edge);
+-------------+
| dst(EDGE)   |
+-------------+
| "team204"   |
| "player101" |
| "player125" |
+-------------+

最后更新: 2024年12月19日