mysql - POSTGRE how to select parent name from hierarchical table -
how select hierarchical table parent id? have table this
+-----------+------------+-------------+ | id | parent_id | name | +-----------+------------+-------------+ |1 | 0 | | +-----------+------------+-------------+ |2 | 1 | a1 | +-----------+------------+-------------+ |3 | 0 | b | +-----------+------------+-------------+ |4 | 3 | b1 | +-----------+------------+-------------+ |5 | 3 | b2 | +-----------+------------+-------------+
and want show table this
+-----------+------------+-------------+ | id | name | parent | +-----------+------------+-------------+ |1 | | null | +-----------+------------+-------------+ |2 | a1 | | +-----------+------------+-------------+ |3 | b | null | +-----------+------------+-------------+ |4 | b1 | b | +-----------+------------+-------------+ |5 | b2 | b | +-----------+------------+-------------+
is possible create select this? know how create in postgre or mysql, please give me suggest,
simply use left join
:
select t1.id, t1.name, t2.name parent yourtable t1 left join yourtable t2 on t1.parent_id = t2.id order t1.id
Comments
Post a Comment