MySql獲取樹型結構的全部子節點

stackoverflow的解決方案,親測有效:sql

SELECT * FROM person WHERE department IN (SELECT department_id FROM department WHERE department_id = 20006 UNION (SELECT department_id FROM (SELECT * FROM department ORDER BY parent_id,department_id) depart_sorted, (SELECT @pv := 20006) initialisation WHERE find_in_set(parent_id,@pv) AND length(@pv := concat(@pv,',',department_id))));

sql:spa

CREATE TABLE department ( id BIGINT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT, department_id INT(11) NOT NULL, parent_id INT(11), department_level INT(2) NOT NULL, department_name VARCHAR(20), attend_group VARCHAR(20) );
相關文章
相關標籤/搜索