Home > Database > Mysql Tutorial > body text

MySQL 递归排序查询+树节点生成

WBOY
Release: 2016-06-07 17:13:26
Original
1482 people have browsed it

mysql 递归排序查询 备注:producttype 排序表,producttype。ptype父节点 ,producttype 。id 主键,showTreeNodes (IN rooti

mysql 递归排序查询

备注:producttype  排序表,producttype。ptype父节点  ,producttype 。id 主键,showTreeNodes (IN rootid INT) 函数,,参数为起始类型rootid.

drop PROCEDURE IF EXISTS  showTreeNodes;

CREATE PROCEDURE showTreeNodes (IN rootid INT)
BEGIN
DECLARE Level int ;
drop TABLE IF EXISTS tmpLst;
CREATE TABLE tmpLst (
  id int,
  nLevel int,
  sCort varchar(8000)
);
Set Level=0 ;
INSERT into tmpLst SELECT id,Level,ID FROM producttype WHERE ptype=rootid;
WHILE ROW_COUNT()>0 DO
  SET Level=Level+1 ;
  INSERT into tmpLst
   SELECT A.ID,Level,concat(B.sCort,A.ID) FROM producttype A,tmpLst B
    WHERE  A.ptype=B.ID AND B.nLevel=Level-1  ;
END WHILE;
END;
CALL showTreeNodes(-1);

SELECT concat(SPACE(B.nLevel*2),'┕',A.name)
FROM producttype A,tmpLst B
WHERE A.ID=B.ID
ORDER BY B.sCort;

linux

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!