RETURN(返回)

在查询的 RETURN 部分,你定义要输出模式的哪些部分。输出可以包括 agtype 值、节点、关系或属性

返回节点

要返回节点,在 RETURN 语句中列出它。

查询

SELECT *
FROM cypher('graph_name', $$
    MATCH (n {name: 'B'})
    RETURN n
$$) as (n agtype);

该示例将返回节点。

结果

n
{id: 0; label: '' properties: {name: 'B'}}::vertex
(1 row)

返回边

要返回 n 的边,只需将其包含在 RETURN 列表中。

查询

SELECT *
FROM cypher('graph_name', $$
    MATCH (n)-[r:KNOWS]->()
    WHERE n.name = 'A'
    RETURN r
$$) as (r agtype);

该示例返回关系。

r
{id: 2; startid: 0; endid: 1; label: 'KNOWS' properties: {}}::edge
(1 row)

返回属性

要返回属性,使用点分隔符,如下所示:

查询

SELECT *
FROM cypher('graph_name', $$
    MATCH (n {name: 'A'})
    RETURN n.name
$$) as (name agtype);

返回属性 name 的值。

结果

name
'A'
(1 row)

返回所有元素

当你想返回查询中找到的所有顶点、边和路径时,可以使用 * 符号。

查询

SELECT *
FROM cypher('graph_name', $$
        MATCH (a {name: 'A'})-[r]->(b)
        RETURN *
$$) as (a agtype, b agtype, r agtype);

返回查询中使用的两个顶点和边。

结果

| a                                                                                                     | b                                                                                                                           | r                                                                         |
| ----------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------- |
| {"id": 281474976710659, "label": "", "properties": {"age": 55, "name": "A", "happy": "Yes!"}}::vertex | {"id": 1125899906842625, "label": "BLOCKS", "end_id": 281474976710660, "start_id": 281474976710659, "properties": {}}::edge | {"id": 281474976710660, "label": "", "properties": {"name": "B"}}::vertex |
| {"id": 281474976710659, "label": "", "properties": {"age": 55, "name": "A", "happy": "Yes!"}}::vertex | {"id": 1407374883553281, "label": "KNOWS", "end_id": 281474976710660, "start_id": 281474976710659, "properties": {}}::edge  | {"id": 281474976710660, "label": "", "properties": {"name": "B"}}::vertex |
(2 rows)                                                                                             

包含特殊字符的变量

要引入由非英文字母字符组成的占位符,可以使用反引号 ` 来包围变量,如下所示:

查询

SELECT *
FROM cypher('graph_name', $$
    MATCH (`This isn\'t a common variable`)
    WHERE `This isn\'t a common variable`.name = 'A'
    RETURN `This isn\'t a common variable`.happy
$$) as (happy agtype);

返回名为 "A" 的节点。

结果

|   happy   |
| --------- |
| "Yes!"    |
 (1 row)   

字段别名

如果字段名称应与使用的表达式不同,可以通过更改列定义列表中的名称来重命名。

查询

SELECT *
FROM cypher('graph_name', $$
    MATCH (n {name: 'A'})
    RETURN n.name
$$) as (objects_name agtype);

返回节点的 age 属性,但重命名了字段。

结果

|   objects_name   |
| ---------------- |
| 'A'              |
 (1 row)          

可选属性

如果属性可能存在也可能不存在,缺失时将被视为 null。

查询

SELECT *
FROM cypher('graph_name', $$
    MATCH (n)
    RETURN n.age
$$) as (age agtype);

如果属性存在则返回该属性,如果不存在则返回 null。

结果

|   age    |
| -------- |
| 55       |
| NULL     |
 (2 rows) 

其他表达式

任何表达式都可以用作返回项——字面量、谓词、属性、函数以及其他所有内容。

查询

SELECT *
FROM cypher('graph_name', $$
    MATCH (a)
    RETURN a.age > 30, 'I'm a literal', id(a)
$$) as (older_than_30 agtype, literal agtype, id agtype);

返回一个谓词、一个字面量和一个带模式表达式参数的函数调用。

结果

|   older_than_30   |   literal       |   id   |
| ----------------- | --------------- | ------ |
| true              | 'I'm a literal' | 1      |
 (1 row)          

去重结果

DISTINCT 根据选定输出的字段仅检索唯一记录。

注意!
尽管每个节点确实有唯一的 id,但 DISTINCT 去重的场景是:同一个节点可能通过不同的边被匹配到多次
举个例子: (A)-[:KNOWS]->(B) (A)-[:LIKES]->(B), 执行 MATCH (a {name: 'A'})-[]->(b) RETURN b 时,b 会匹配到 B 两次(一次通过 KNOWS 边,一次通过 LIKES 边),返回两行相同的 B。 加了 DISTINCT 后,B 只返回一次。

查询

SELECT *
FROM cypher('graph_name', $$
MATCH (a {name: 'A'})-[]->(b)
RETURN DISTINCT b
$$) as (b agtype);

查询返回名为 "B" 的节点,但只返回一次。

结果

|   b                                                |
| -------------------------------------------------- |
| {id: 1; label: '' properties: {name: 'B'}}::vertex |
 (1 row)