程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> mysql遞歸查詢樹形表

mysql遞歸查詢樹形表

編輯:MySQL綜合教程

mysql遞歸查詢樹形表


DROP TABLE IF EXISTS `t_areainfo`;
CREATE TABLE `t_areainfo` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `level` int(11) DEFAULT 0,
 `name` varchar(255),
 `parentId` int(11),
 `status` int(11) DEFAULT 0,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=65 DEFAULT CHARSET=utf8;


INSERT INTO `t_areainfo` VALUES ('1', '0', '中國', '0', '0');
INSERT INTO `t_areainfo` VALUES ('2', '0', '華北區', '1', '0');
INSERT INTO `t_areainfo` VALUES ('3', '0', '華南區', '1', '0');
INSERT INTO `t_areainfo` VALUES ('4', '0', '北京', '2', '0');
INSERT INTO `t_areainfo` VALUES ('5', '0', '海澱區', '4', '0');
INSERT INTO `t_areainfo` VALUES ('6', '0', '豐台區', '4', '0');
INSERT INTO `t_areainfo` VALUES ('7', '0', '朝陽區', '4', '0');
INSERT INTO `t_areainfo` VALUES ('8', '0', '北京XX區1', '4', '0');
INSERT INTO `t_areainfo` VALUES ('9', '0', '北京XX區2', '4', '0');
INSERT INTO `t_areainfo` VALUES ('10', '0', '北京XX區3', '4', '0');
INSERT INTO `t_areainfo` VALUES ('11', '0', '北京XX區4', '4', '0');
INSERT INTO `t_areainfo` VALUES ('12', '0', '北京XX區5', '4', '0');
INSERT INTO `t_areainfo` VALUES ('13', '0', '北京XX區6', '4', '0');
INSERT INTO `t_areainfo` VALUES ('14', '0', '北京XX區7', '4', '0');
INSERT INTO `t_areainfo` VALUES ('15', '0', '北京XX區8', '4', '0');
INSERT INTO `t_areainfo` VALUES ('16', '0', '北京XX區9', '4', '0');
INSERT INTO `t_areainfo` VALUES ('17', '0', '北京XX區10', '4', '0');
INSERT INTO `t_areainfo` VALUES ('18', '0', '北京XX區11', '4', '0');
INSERT INTO `t_areainfo` VALUES ('19', '0', '北京XX區12', '4', '0');
INSERT INTO `t_areainfo` VALUES ('20', '0', '北京XX區13', '4', '0');
INSERT INTO `t_areainfo` VALUES ('21', '0', '北京XX區14', '4', '0');
INSERT INTO `t_areainfo` VALUES ('22', '0', '北京XX區15', '4', '0');
INSERT INTO `t_areainfo` VALUES ('23', '0', '北京XX區16', '4', '0');
INSERT INTO `t_areainfo` VALUES ('24', '0', '北京XX區17', '4', '0');
INSERT INTO `t_areainfo` VALUES ('25', '0', '北京XX區18', '4', '0');
INSERT INTO `t_areainfo` VALUES ('26', '0', '北京XX區19', '4', '0');
INSERT INTO `t_areainfo` VALUES ('27', '0', '北京XX區1', '4', '0');
INSERT INTO `t_areainfo` VALUES ('28', '0', '北京XX區2', '4', '0');
INSERT INTO `t_areainfo` VALUES ('29', '0', '北京XX區3', '4', '0');
INSERT INTO `t_areainfo` VALUES ('30', '0', '北京XX區4', '4', '0');
INSERT INTO `t_areainfo` VALUES ('31', '0', '北京XX區5', '4', '0');
INSERT INTO `t_areainfo` VALUES ('32', '0', '北京XX區6', '4', '0');
INSERT INTO `t_areainfo` VALUES ('33', '0', '北京XX區7', '4', '0');
INSERT INTO `t_areainfo` VALUES ('34', '0', '北京XX區8', '4', '0');
INSERT INTO `t_areainfo` VALUES ('35', '0', '北京XX區9', '4', '0');
INSERT INTO `t_areainfo` VALUES ('36', '0', '北京XX區10', '4', '0');
INSERT INTO `t_areainfo` VALUES ('37', '0', '北京XX區11', '4', '0');
INSERT INTO `t_areainfo` VALUES ('38', '0', '北京XX區12', '4', '0');
INSERT INTO `t_areainfo` VALUES ('39', '0', '北京XX區13', '4', '0');
INSERT INTO `t_areainfo` VALUES ('40', '0', '北京XX區14', '4', '0');
INSERT INTO `t_areainfo` VALUES ('41', '0', '北京XX區15', '4', '0');
INSERT INTO `t_areainfo` VALUES ('42', '0', '北京XX區16', '4', '0');
INSERT INTO `t_areainfo` VALUES ('43', '0', '北京XX區17', '4', '0');
INSERT INTO `t_areainfo` VALUES ('44', '0', '北京XX區18', '4', '0');
INSERT INTO `t_areainfo` VALUES ('45', '0', '北京XX區19', '4', '0');
INSERT INTO `t_areainfo` VALUES ('46', '0', 'xx省1', '1', '0');
INSERT INTO `t_areainfo` VALUES ('47', '0', 'xx省2', '1', '0');
INSERT INTO `t_areainfo` VALUES ('48', '0', 'xx省3', '1', '0');
INSERT INTO `t_areainfo` VALUES ('49', '0', 'xx省4', '1', '0');
INSERT INTO `t_areainfo` VALUES ('50', '0', 'xx省5', '1', '0');
INSERT INTO `t_areainfo` VALUES ('51', '0', 'xx省6', '1', '0');
INSERT INTO `t_areainfo` VALUES ('52', '0', 'xx省7', '1', '0');
INSERT INTO `t_areainfo` VALUES ('53', '0', 'xx省8', '1', '0');
INSERT INTO `t_areainfo` VALUES ('54', '0', 'xx省9', '1', '0');
INSERT INTO `t_areainfo` VALUES ('55', '0', 'xx省10', '1', '0');
INSERT INTO `t_areainfo` VALUES ('56', '0', 'xx省11', '1', '0');
INSERT INTO `t_areainfo` VALUES ('57', '0', 'xx省12', '1', '0');
INSERT INTO `t_areainfo` VALUES ('58', '0', 'xx省13', '1', '0');
INSERT INTO `t_areainfo` VALUES ('59', '0', 'xx省14', '1', '0');
INSERT INTO `t_areainfo` VALUES ('60', '0', 'xx省15', '1', '0');
INSERT INTO `t_areainfo` VALUES ('61', '0', 'xx省16', '1', '0');
INSERT INTO `t_areainfo` VALUES ('62', '0', 'xx省17', '1', '0');
INSERT INTO `t_areainfo` VALUES ('63', '0', 'xx省18', '1', '0');
INSERT INTO `t_areainfo` VALUES ('64', '0', 'xx省19', '1', '0');


--方式一:采用function獲取所有子節點的id
--查詢傳入areaId及其以下所有子節點

delimiter //
DROP FUNCTION IF EXISTS queryChildrenAreaInfo;
CREATE FUNCTION `queryChildrenAreaInfo` (areaId INT)
RETURNS VARCHAR(4000)
BEGIN
DECLARE sTemp VARCHAR(4000);
DECLARE sTempChd VARCHAR(4000);

SET sTemp = '$';
SET sTempChd = cast(areaId as char);

WHILE sTempChd is not NULL DO
SET sTemp = CONCAT(sTemp,',',sTempChd);
SELECT group_concat(id) INTO sTempChd FROM t_areainfo where FIND_IN_SET(parentId,sTempChd)>0;
END WHILE;
return sTemp;
END
//
delimiter //


2
--調用方式
select queryChildrenAreaInfo(1);
select * from t_areainfo where FIND_IN_SET(id, queryChildrenAreaInfo(20)); 

/*

delimiter //

CREATE FUNCTION `queryChildrenAreaInfo`(rootId INT)
RETURNS varchar(1000)
BEGIN
DECLARE sTemp VARCHAR(1000);
DECLARE sTempChd VARCHAR(1000);

SET sTemp = '$';
SET sTempChd =cast(rootId as CHAR);

WHILE sTempChd is not null DO
SET sTemp = concat(sTemp,',',sTempChd);
SELECT group_concat(id) INTO sTempChd FROM t_areainfo where FIND_IN_SET(parentId,sTempChd)>0;
END WHILE;
RETURN sTemp;
END
//

delimiter ;

*/

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved