Union
本页描述如何使用 Cypher®.Union 类创建 UNION 子句。
通过向 Cypher.Union 类传入多个子句来创建多个查询的并集。请注意,所有子句必须是不同的实例,因为 Cypher Builder 子句不能重复使用。
const returnVar = new Cypher.Variable();
const n1 = new Cypher.Node();
const query1 = new Cypher.Match(new Cypher.Pattern(n1, { labels: ["Movie"] })).return([n1, returnVar]);
const n2 = new Cypher.Node();
const query2 = new Cypher.Match(new Cypher.Pattern(n2, { labels: ["Movie"] })).return([n2, returnVar]);
const n3 = new Cypher.Node();
const query3 = new Cypher.Match(new Cypher.Pattern(n3, { labels: ["Movie"] })).return([n3, returnVar]);
const unionQuery = new Cypher.Union(query1, query2, query3);
这将生成以下 UNION 子句
MATCH (this0:Movie)
RETURN this0 AS var1
UNION
MATCH (this2:Movie)
RETURN this2 AS var1
UNION
MATCH (this3:Movie)
RETURN this3 AS var1
请注意,此示例使用额外变量 returnVar,以确保所有查询返回相同的变量名称。
UNION ALL
UNION 默认会去除重复项。若要保留全部结果,请使用 .all() 方法来创建 UNION ALL 子句。
const returnVar = new Cypher.Variable();
const n1 = new Cypher.Node();
const query1 = new Cypher.Match(new Cypher.Pattern(n1, { labels: ["Movie"] })).return([n1, returnVar]);
const n2 = new Cypher.Node();
const query2 = new Cypher.Match(new Cypher.Pattern(n2, { labels: ["Movie"] })).return([n2, returnVar]);
const n3 = new Cypher.Node();
const query3 = new Cypher.Match(new Cypher.Pattern(n3, { labels: ["Movie"] })).return([n3, returnVar]);
const unionQuery = new Cypher.Union(query1, query2, query3).all();
这将生成以下 UNION 子句
MATCH (this0:Movie)
RETURN this0 AS var1
UNION ALL
MATCH (this2:Movie)
RETURN this2 AS var1
UNION ALL
MATCH (this3:Movie)
RETURN this3 AS var1