Neo4j Graph Database Fundamentals · 课时

筛选与排序结果

使用 WHERE 和 ORDER BY 子句优化查询结果,确保准确获取所需数据

第 1 / 4 课11 个步骤

筛选与排序结果 是 CoddyKit 上的免费 Neo4j Graph Database Fundamentals 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Neo4j Graph Database Fundamentals 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Neo4j Graph Database Fundamentals 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Refine Your Graph Queries

When working with large graphs, you often need to find very specific information or present it in a particular order.

  • Filtering helps you narrow down results to only what's relevant.
  • Ordering helps you sort your results for better readability or analysis.

In this lesson, we'll master the WHERE and ORDER BY clauses in Cypher.

Filter Results with WHERE

The WHERE clause is used to filter patterns or properties based on specific conditions. It typically comes after a MATCH clause.

Think of it like saying: "Find me all people, WHERE their birth year is 1970."

MATCH (p:Person)
WHERE p.born = 1970
RETURN p.name AS Name, p.born AS BornYear

Comparison Operators

You can use various comparison operators in your WHERE clause:

  • = (equal to)
  • <> (not equal to)
  • > (greater than)
  • < (less than)
  • >= (greater than or equal to)
  • <= (less than or equal to)

Let's find movies released after the year 2000.

MATCH (m:Movie)
WHERE m.released > 2000
RETURN m.title AS MovieTitle, m.released AS ReleaseYear

Logical Operators (AND, OR)

Combine multiple conditions using logical operators:

  • AND: Both conditions must be true.
  • OR: At least one condition must be true.
  • XOR: Exactly one condition must be true.
  • NOT: Negates a condition.

Find people born between 1970 and 1980 (inclusive).

MATCH (p:Person)
WHERE p.born >= 1970 AND p.born <= 1980
RETURN p.name AS Name, p.born AS BornYear

String and List Operators

Cypher offers powerful operators for string matching and list containment:

  • CONTAINS, STARTS WITH, ENDS WITH for partial string matches.
  • IN to check if a property value is within a list of values.

Let's find specific actors by name using IN.

MATCH (p:Person)
WHERE p.name IN ['Tom Hanks', 'Keanu Reeves', 'Carrie-Anne Moss']
RETURN p.name AS ActorName

Sort Your Results with ORDER BY

The ORDER BY clause sorts your query results. It comes after RETURN (or WITH).

By default, results are sorted in ascending order (smallest to largest, A-Z).

MATCH (p:Person)
RETURN p.name AS ActorName
ORDER BY ActorName

Ascending & Descending Order

You can explicitly specify the sort direction:

  • ASC: Ascending (A-Z, 0-9). This is the default.
  • DESC: Descending (Z-A, 9-0).

Let's sort movies by their release year, from newest to oldest.

MATCH (m:Movie)
RETURN m.title AS MovieTitle, m.released AS ReleaseYear
ORDER BY ReleaseYear DESC

Multi-Level Sorting

You can sort by multiple properties. The order in which you list them in ORDER BY matters.

Cypher will first sort by the first property, then by the second for any ties in the first, and so on.

Sort people by birth year (ascending), and then by name (ascending) for those born in the same year.

MATCH (p:Person)
RETURN p.born AS BornYear, p.name AS ActorName
ORDER BY BornYear ASC, ActorName ASC

Putting it All Together

A typical Cypher query flow often involves:

  1. MATCH: Find the graph pattern.
  2. WHERE: Filter the matched patterns.
  3. RETURN: Select the data you want.
  4. ORDER BY: Sort the returned data.

Find actors born before 1970, order them by name (descending), then by the movie title they acted in (ascending).

MATCH (p:Person)-[:ACTED_IN]->(m:Movie)
WHERE p.born < 1970
RETURN p.name AS Actor, m.title AS Movie, p.born AS BornYear
ORDER BY Actor DESC, Movie ASC

Filter & Order Challenge

Which Cypher query correctly finds movies released after 1999 and orders them by title in descending order?

Recap: Refined Queries

Great job! You've learned how to precisely control your Cypher query results:

  • The WHERE clause filters data based on conditions.
  • The ORDER BY clause sorts your results in ascending or descending order.
  • You can combine these with comparison, logical, and string/list operators for powerful filtering.

Next, we'll explore how to summarize and reshape your data with Aggregation and Projections!

免费开始

用 AI 导师学习 Neo4j Graph Database Fundamentals — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
12
课程
48

常见问题解答

「筛选与排序结果」课时是免费的吗?

是的 — 「筛选与排序结果」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Neo4j Graph Database Fundamentals 课程的其余内容,请升级到 CoddyKit PRO。 Neo4j Graph Database Fundamentals 课程共包含 4 节课。

「筛选与排序结果」这节课中我会学到什么?

使用 WHERE 和 ORDER BY 子句优化查询结果,确保准确获取所需数据 你通过在浏览器中直接运行的动手代码来练习 Neo4j Graph Database Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Neo4j Graph Database Fundamentals 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Neo4j Graph Database Fundamentals 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「筛选与排序结果」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Neo4j Graph Database Fundamentals 课中编写并运行代码吗?

能。每节 Neo4j Graph Database Fundamentals 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 筛选与排序结果
  2. 聚合与投影
  3. 更新与删除图数据
  4. 使用 WITH 和 UNION 组合查询
← 返回 Neo4j Graph Database Fundamentals