Neo4j 简明教程
Neo4j - Where Clause
与 SQL 类似,Neo4j CQL 在 CQL MATCH 命令中提供了 WHERE 子句以过滤 MATCH 查询的结果。
Like SQL, Neo4j CQL has provided WHERE clause in CQL MATCH command to filter the results of a MATCH Query.
Syntax
以下是 WHERE 子句的语法。
Following is the syntax of the WHERE clause.
MATCH (label)
WHERE label.country = "property"
RETURN label
Example
在继续执行示例之前,请按照以下方式在数据库中创建五个节点。
Before proceeding with the example, create five nodes in the database as shown below.
CREATE(Dhawan:player{name:"shikar Dhawan", YOB: 1985, runs:363, country: "India"}
CREATE(Jonathan:player{name:"Jonathan Trott", YOB:1981, runs:229, country:"South Africa"}
CREATE(Sangakkara:player{name:"Kumar Sangakkara", YOB:1977, runs:222,
country:"Srilanka"})
CREATE(Rohit:player{name:"Rohit Sharma", YOB: 1987, runs:177, country:"India"})
CREATE(Virat:player{name:"Virat Kohli", YOB: 1988, runs:176, country:"India"})
CREATE(Ind:Country {name: "India", result: "Winners"})
以下是使用 WHERE 子句返回所有属于印度的球员(节点)的示例 Cypher 查询。
Following is a sample Cypher Query which returns all the players (nodes) that belongs to the country India using WHERE clause.
MATCH (player)
WHERE player.country = "India"
RETURN player
执行上述查询,执行以下步骤:
To execute the above query, carry out the following steps −
Step 1 - 打开 Neo4j Desktop App 并启动 Neo4j Server。使用 URL http://localhost:7474/ 打开 Neo4j 的内置浏览器应用,如下面的屏幕截图所示。
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in browser app of Neo4j using the URL http://localhost:7474/ as shown in the following screenshot.

Step 2 - 在美元提示符中复制并粘贴所需的查询,并按播放按钮(执行查询)突出显示在下面的屏幕截图中。
Step 2 − Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highlighted in the following screenshot.

WHERE Clause with Multiple Conditions
您还可以使用 WHERE 子句验证多个条件。
You can also use the WHERE clause to verify multiple conditions.
Syntax
以下是具有多个条件在 Neo4j 中使用 WHERE 子句的语法。
Following is the syntax to use WHERE clause in Neo4j with multiple conditions.
MATCH (emp:Employee)
WHERE emp.name = 'Abc' AND emp.name = 'Xyz'
RETURN emp
Example
以下是使用两种条件在 Neo4j 数据库中过滤节点的示例 Cypher 查询。
Following is a sample Cypher Query which filters the nodes in the Neo4j database using two conditions.
MATCH (player)
WHERE player.country = "India" AND player.runs >=175
RETURN player
执行上述查询,执行以下步骤:
To execute the above query, carry out the following steps −
Step 1 - 打开 Neo4j Desktop App 并启动 Neo4j Server。使用 URL http://localhost:7474/ 打开 Neo4j 的内置浏览器应用,如下面的屏幕截图所示。
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in browser app of Neo4j using the URL http://localhost:7474/ as shown in the following screenshot.

Step 2 - 在美元提示符中复制并粘贴所需的查询,并按播放按钮(执行查询)突出显示在下面的屏幕截图中。
Step 2 − Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highlighted in the following screenshot.

Using Relationship with Where Clause
您还可以使用 Where 子句使用关系来过滤节点。
You can also use Where clause to filter the nodes using the relationships.
Example
假设我们有数据库中的以下图表。
Assume we have the following graph in the database.

以下是如下所示使用 WHERE 子句检索印度最高得分的示例 Cypher 查询。
Following is a sample Cypher Query to retrieve the top scorer of India using WHERE clause as shown below.
MATCH (n)
WHERE (n)-[: TOP_SCORER_OF]->( {name: "India", result: "Winners"})
RETURN n
执行上述查询,执行以下步骤:
To execute the above query, carry out the following steps −
Step 1 - 打开 Neo4j Desktop App 并启动 Neo4j Server。使用 URL http://localhost:7474/ 打开 Neo4j 的内置浏览器应用,如下面的屏幕截图所示。
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in browser app of Neo4j using the URL http://localhost:7474/ as shown in the following screenshot.

Step 2 - 在美元提示符中复制并粘贴所需的查询,并按播放按钮(执行查询)突出显示在下面的屏幕截图中。
Step 2 − Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highlighted in the following screenshot.
