树形查询sql

 

DROP FUNCTION IF EXISTS PersonName;

CREATE FUNCTION PersonName(parent_id smallint) RETURNS VARCHAR(20) DETERMINISTIC

RETURN (SELECT job_name FROM qx_people_resource WHERE id_=parent_id);

 

SELECT PersonName(id_) AS child, PersonName(parent_id) AS parent

FROM qx_people_resource;

 

参考:http://www.artfulsoftware.com/mysqlbook/sampler/mysqled1ch20.html#adjacency_list_model

posted @ 2018-01-22 20:26  秋水秋色  阅读(1082)  评论(0编辑  收藏  举报