使用 WITH 和 UNION 组合查询
学习使用 WITH 串联查询阶段,并使用 UNION 合并结果集,以进行高级 Cypher 查询。
使用 WITH 和 UNION 组合查询 是 CoddyKit 上的免费 Neo4j Graph Database Fundamentals 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Neo4j Graph Database Fundamentals 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Neo4j Graph Database Fundamentals 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Why Chain Query Stages
Complex questions often need several steps: filter, aggregate, then filter again. Cypher uses WITH to pass results from one stage to the next.
Introducing WITH
WITH works like RETURN but feeds its output into the next part of the query instead of ending it.
MATCH (p:Person)-[:FRIEND]->(f)
WITH p, count(f) AS friendCount
WHERE friendCount > 3
RETURN p.name, friendCount;Filtering After Aggregation
You cannot put aggregate functions in a WHERE directly. Instead, aggregate with WITH, then filter the aggregated value in the following WHERE.
This is the Cypher equivalent of SQL's HAVING.
MATCH (c:City)<-[:LIVES_IN]-(p:Person)
WITH c, count(p) AS population
WHERE population > 1000
RETURN c.name, population;Carrying Variables Forward
Only variables listed in WITH stay in scope afterward. Anything omitted is dropped, so list everything you still need.
MATCH (p:Person)-[:BOUGHT]->(prod)
WITH p, collect(prod.name) AS products
RETURN p.name, products;Ordering and Limiting Mid-Query
WITH can include ORDER BY, SKIP, and LIMIT to shape intermediate results before the next stage.
MATCH (p:Person)
WITH p ORDER BY p.age DESC LIMIT 5
MATCH (p)-[:FRIEND]->(f)
RETURN p.name, f.name;Introducing UNION
UNION combines the results of two queries into one result set. Both sides must return the same column names.
MATCH (p:Person) RETURN p.name AS name
UNION
MATCH (c:Company) RETURN c.name AS name;UNION vs UNION ALL
UNION removes duplicate rows. UNION ALL keeps every row, including duplicates, and is faster when you know there are none.
MATCH (p:Person) RETURN p.name AS name
UNION ALL
MATCH (c:Company) RETURN c.name AS name;Combining WITH and UNION
You can aggregate within each side of a UNION using WITH, then merge the summaries. This builds powerful reporting queries.
MATCH (o:Order) WITH 'orders' AS kind, count(o) AS n RETURN kind, n
UNION
MATCH (u:User) WITH 'users' AS kind, count(u) AS n RETURN kind, n;Multiple WITH Stages
You can chain many WITH clauses to build a pipeline: each stage refines the data further.
MATCH (p:Person)-[:RATED]->(m:Movie)
WITH m, avg(p.rating) AS avgRating
WITH m, avgRating WHERE avgRating > 4
RETURN m.title, avgRating;Common Pitfalls
Watch out for:
- Forgetting to carry a needed variable through WITH
- Mismatched column names across UNION branches
- Using WITH when a simple WHERE would do
When to Use Each
Use WITH for multi-step pipelines and post-aggregation filtering. Use UNION to stack results from structurally different queries.
Quick Check
Check your understanding of query composition.
Recap
You learned to compose advanced queries:
- WITH pipes results between stages
- Filter aggregates after WITH (like HAVING)
- Only variables in WITH stay in scope
- UNION merges result sets; UNION ALL keeps duplicates
常见问题解答
「使用 WITH 和 UNION 组合查询」课时是免费的吗?
是的 — 「使用 WITH 和 UNION 组合查询」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Neo4j Graph Database Fundamentals 课程的其余内容,请升级到 CoddyKit PRO。 Neo4j Graph Database Fundamentals 课程共包含 4 节课。
「使用 WITH 和 UNION 组合查询」这节课中我会学到什么?
学习使用 WITH 串联查询阶段,并使用 UNION 合并结果集,以进行高级 Cypher 查询。 你通过在浏览器中直接运行的动手代码来练习 Neo4j Graph Database Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Neo4j Graph Database Fundamentals 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Neo4j Graph Database Fundamentals 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「使用 WITH 和 UNION 组合查询」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Neo4j Graph Database Fundamentals 课中编写并运行代码吗?
能。每节 Neo4j Graph Database Fundamentals 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。