get a recursive parent list

后端 未结 2 1546
太阳男子
太阳男子 2020-12-01 17:06

Using MySQL, I want to return a list of parents, from a table that has a field structure like this. ID,PARENTID,NAME (a standard parent-child hierarchy). I would like to tra

2条回答
  •  臣服心动
    2020-12-01 17:55

    In this example we are checking 5 levels up:

    select 
        t1.parentid, t2.parentid, t3.parentid, t4.parentid, t5.parentid
    from
        tableName t1
        left join tableName t2 on t1.parentid = t2.id
        left join tableName t3 on t2.parentid = t3.id
        left join tableName t4 on t3.parentid = t4.id
        left join tableName t5 on t4.parentid = t5.id
    where
        t1.name = 'thing3'
    

提交回复
热议问题