Search
 
SCRIPT & CODE EXAMPLE
 

SQL

sql parent child tree query

WITH RECURSIVE generation AS (
    SELECT id,
        first_name,
        last_name,
        parent_id,
        0 AS generation_number
    FROM parent_child
    WHERE parent_id IS NULL
 
UNION ALL
 
    SELECT child.id,
        child.first_name,
        child.last_name,
        child.parent_id,
        generation_number+1 AS generation_number
    FROM parent_child child
    JOIN generation g
      ON g.id = child.parent_id
)
 
SELECT first_name,
     last_name,
     generation_number
FROM generation;
Comment

PREVIOUS NEXT
Code Example
Sql :: median mysql 
Sql :: how to change db owner postgres 
Sql :: how to select an index in oracle sql 
Sql :: create scalar function in sql server 
Sql :: create tablespace oracle multiple datafiles 
Sql :: oracle array 
Sql :: sql convert date format 
Sql :: python dictionary to sql update 
Sql :: sql server md5 hash 
Sql :: average salary in sql 
Sql :: dba_dependencies 
Sql :: rename column name sql server 
Sql :: postgres dump sql insert 
Sql :: drop function in sql 
Sql :: sql server query database space available 
Sql :: sql server pivot rows to columns 
Sql :: sql auto_increment syntax 
Sql :: postgresql default value 
Sql :: mysql biginteger size 
Sql :: sql select if not exists 
Sql :: sql right join 
Sql :: python mysql create table if not exists 
Sql :: SQL Updating a View 
Sql :: warning: mysqli::__construct(): (hy000/2002): 
Sql :: database stuck on restoring 
Sql :: Which MySQL data type to use for storing boolean values 
Sql :: mysql not empty 
Sql :: sql restore database from backup 
Sql :: show function mysql 
Sql :: php get closest location by latitude longitude 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =