การรวมคำค้นด้วย WITH และ UNION
เรียนรู้การเชื่อมขั้นตอนของคำค้นด้วย WITH และผสานชุดผลลัพธ์ด้วย UNION สำหรับการเขียนคำค้น Cypher ขั้นสูง
การรวมคำค้นด้วย WITH และ UNION เป็นบทเรียน Neo4j Graph Database Fundamentals ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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
เรียนรู้ Neo4j Graph Database Fundamentals ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “การรวมคำค้นด้วย WITH และ UNION” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การรวมคำค้นด้วย WITH และ UNION” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Neo4j Graph Database Fundamentals ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Neo4j Graph Database Fundamentals มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การรวมคำค้นด้วย WITH และ UNION”
เรียนรู้การเชื่อมขั้นตอนของคำค้นด้วย WITH และผสานชุดผลลัพธ์ด้วย UNION สำหรับการเขียนคำค้น Cypher ขั้นสูง คุณปฏิบัติ Neo4j Graph Database Fundamentals ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Neo4j Graph Database Fundamentals หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Neo4j Graph Database Fundamentals บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การรวมคำค้นด้วย WITH และ UNION” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Neo4j Graph Database Fundamentals นี้ได้ไหม
ได้ บทเรียน Neo4j Graph Database Fundamentals ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การกรองและจัดเรียงผลลัพธ์
- การรวมข้อมูลและการฉายข้อมูล
- การอัปเดตและลบข้อมูลกราฟ
- การรวมคำค้นด้วย WITH และ UNION