sql >> Database >  >> RDS >> Sqlserver

SQL recursieve query die alle voorouders van een item ophaalt

with name_tree as (
   select id, parent_id, name
   from the_unknown_table
   where id = 1 -- this is the starting point you want in your recursion
   union all
   select c.id, c.parent_id, c.name
   from the_unknown_table c
     join name_tree p on p.parent_id = c.id  -- this is the recursion
) 
select *
from name_tree
where id <> 1; -- exclude the starting point from the overall result

SQLFiddle:http://sqlfiddle.com/#!3/87d0c/1



  1. Microsoft Access Tips en trucs deel 2 – Formulieren

  2. Mysql join vanuit meerdere tabellen

  3. Neem de controle over uw gegevens met Microsoft Access

  4. Hoe controleer ik of de waarde al in de tabel bestaat?