All Products
Search
Document Center

PolarDB:Multi-label query

Last Updated:Feb 13, 2025

Labels are used to represent the properties of nodes and edges. You can categorize the nodes and edges based on the semantic meaning of properties.

Specify multiple labels in a statement

Separate multiple labels by using | as shown in the following sample code:

(n:Person|Company)
[p:KNOWS|LIKES]

Prepare test data

SELECT create_graph('imdb');
SELECT *
FROM cypher('imdb', $$
 CREATE (toby:actor {name: 'Toby Maguire'}),
  (willam:director {name: 'Willam Dafoe'}),
  (hanks:person {name: 'Tom Hanks'}),
  (spiderman:movie {title: 'Spiderman'}),
  (toby)-[:acted_in]->(spiderman),
  (willam)-[:directed]->(spiderman),
  (hanks)-[:likes]->(spiderman)
$$) AS (a agtype);

Examples

  • Query the names with either the actor or director label.

    SELECT * FROM cypher('imdb', $$
    MATCH (a:actor|director) 
    RETURN a.name
    $$
    ) as (name agtype);

    Sample result:

          name      
    ----------------
     "Toby Maguire"
     "Willam Dafoe"
    (2 rows)
  • Query the individuals who are related to the movie Spiderman in either the directed or acted_in relationship.

    SELECT * FROM cypher('imdb', $$
    MATCH (p)-[r:acted_in|directed]->(:movie {title: 'Spiderman'}) 
    RETURN p.name
    $$
    ) as (name agtype);

    Sample result:

          name      
    ----------------
     "Toby Maguire"
     "Willam Dafoe"
    (2 rows)