真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

mysql怎樣遞歸查詢所有數(shù)據(jù)

小編給大家分享一下MySQL怎樣遞歸查詢所有數(shù)據(jù),相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

創(chuàng)新互聯(lián)公司專注于網(wǎng)站建設(shè)|企業(yè)網(wǎng)站維護(hù)|優(yōu)化|托管以及網(wǎng)絡(luò)推廣,積累了大量的網(wǎng)站設(shè)計(jì)與制作經(jīng)驗(yàn),為許多企業(yè)提供了網(wǎng)站定制設(shè)計(jì)服務(wù),案例作品覆蓋成都不銹鋼雕塑等行業(yè)。能根據(jù)企業(yè)所處的行業(yè)與銷售的產(chǎn)品,結(jié)合品牌形象的塑造,量身設(shè)計(jì)品質(zhì)網(wǎng)站。

mysql遞歸查詢所有數(shù)據(jù)的方法:首先創(chuàng)建表,并初始數(shù)據(jù);然后利用【find_in_set()】函數(shù)和【group_concat()】函數(shù)實(shí)現(xiàn)遞歸查詢,代碼為【SELECT queryChildrenAreaInfo(1);】。

mysql遞歸查詢所有數(shù)據(jù)的方法:

1、創(chuàng)建表

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

2、初始數(shù)據(jù)

INSERT INTO `t_areainfo` VALUES ('1', '0', '中國', '0', '0');
INSERT INTO `t_areainfo` VALUES ('2', '0', '華北區(qū)', '1', '0');
INSERT INTO `t_areainfo` VALUES ('3', '0', '華南區(qū)', '1', '0');
INSERT INTO `t_areainfo` VALUES ('4', '0', '北京', '2', '0');
INSERT INTO `t_areainfo` VALUES ('5', '0', '海淀區(qū)', '4', '0');
INSERT INTO `t_areainfo` VALUES ('6', '0', '豐臺(tái)區(qū)', '4', '0');
INSERT INTO `t_areainfo` VALUES ('7', '0', '朝陽區(qū)', '4', '0');
INSERT INTO `t_areainfo` VALUES ('8', '0', '北京XX區(qū)1', '4', '0');
INSERT INTO `t_areainfo` VALUES ('9', '0', '北京XX區(qū)2', '4', '0');
INSERT INTO `t_areainfo` VALUES ('10', '0', '北京XX區(qū)3', '4', '0');
INSERT INTO `t_areainfo` VALUES ('11', '0', '北京XX區(qū)4', '4', '0');
INSERT INTO `t_areainfo` VALUES ('12', '0', '北京XX區(qū)5', '4', '0');
INSERT INTO `t_areainfo` VALUES ('13', '0', '北京XX區(qū)6', '4', '0');
INSERT INTO `t_areainfo` VALUES ('14', '0', '北京XX區(qū)7', '4', '0');
INSERT INTO `t_areainfo` VALUES ('15', '0', '北京XX區(qū)8', '4', '0');
INSERT INTO `t_areainfo` VALUES ('16', '0', '北京XX區(qū)9', '4', '0');
INSERT INTO `t_areainfo` VALUES ('17', '0', '北京XX區(qū)10', '4', '0');
INSERT INTO `t_areainfo` VALUES ('18', '0', '北京XX區(qū)11', '4', '0');
INSERT INTO `t_areainfo` VALUES ('19', '0', '北京XX區(qū)12', '4', '0');
INSERT INTO `t_areainfo` VALUES ('20', '0', '北京XX區(qū)13', '4', '0');
INSERT INTO `t_areainfo` VALUES ('21', '0', '北京XX區(qū)14', '4', '0');
INSERT INTO `t_areainfo` VALUES ('22', '0', '北京XX區(qū)15', '4', '0');
INSERT INTO `t_areainfo` VALUES ('23', '0', '北京XX區(qū)16', '4', '0');
INSERT INTO `t_areainfo` VALUES ('24', '0', '北京XX區(qū)17', '4', '0');
INSERT INTO `t_areainfo` VALUES ('25', '0', '北京XX區(qū)18', '4', '0');
INSERT INTO `t_areainfo` VALUES ('26', '0', '北京XX區(qū)19', '4', '0');
INSERT INTO `t_areainfo` VALUES ('27', '0', '北京XX區(qū)1', '4', '0');
INSERT INTO `t_areainfo` VALUES ('28', '0', '北京XX區(qū)2', '4', '0');
INSERT INTO `t_areainfo` VALUES ('29', '0', '北京XX區(qū)3', '4', '0');
INSERT INTO `t_areainfo` VALUES ('30', '0', '北京XX區(qū)4', '4', '0');
INSERT INTO `t_areainfo` VALUES ('31', '0', '北京XX區(qū)5', '4', '0');
INSERT INTO `t_areainfo` VALUES ('32', '0', '北京XX區(qū)6', '4', '0');
INSERT INTO `t_areainfo` VALUES ('33', '0', '北京XX區(qū)7', '4', '0');
INSERT INTO `t_areainfo` VALUES ('34', '0', '北京XX區(qū)8', '4', '0');
INSERT INTO `t_areainfo` VALUES ('35', '0', '北京XX區(qū)9', '4', '0');
INSERT INTO `t_areainfo` VALUES ('36', '0', '北京XX區(qū)10', '4', '0');
INSERT INTO `t_areainfo` VALUES ('37', '0', '北京XX區(qū)11', '4', '0');
INSERT INTO `t_areainfo` VALUES ('38', '0', '北京XX區(qū)12', '4', '0');
INSERT INTO `t_areainfo` VALUES ('39', '0', '北京XX區(qū)13', '4', '0');
INSERT INTO `t_areainfo` VALUES ('40', '0', '北京XX區(qū)14', '4', '0');
INSERT INTO `t_areainfo` VALUES ('41', '0', '北京XX區(qū)15', '4', '0');
INSERT INTO `t_areainfo` VALUES ('42', '0', '北京XX區(qū)16', '4', '0');
INSERT INTO `t_areainfo` VALUES ('43', '0', '北京XX區(qū)17', '4', '0');
INSERT INTO `t_areainfo` VALUES ('44', '0', '北京XX區(qū)18', '4', '0');
INSERT INTO `t_areainfo` VALUES ('45', '0', '北京XX區(qū)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');

3、向下遞歸

利用find_in_set()函數(shù)和group_concat()函數(shù)實(shí)現(xiàn)遞歸查詢:

DROP FUNCTION IF EXISTS queryChildrenAreaInfo;
DELIMITER ;;
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 ;

4、調(diào)用方式

SELECT queryChildrenAreaInfo(1);

mysql怎樣遞歸查詢所有數(shù)據(jù)

查詢id為"4"下面的所有節(jié)點(diǎn)

SELECT * FROM t_areainfo WHERE FIND_IN_SET(id,queryChildrenAreaInfo(4));

mysql怎樣遞歸查詢所有數(shù)據(jù)

5、向上遞歸

DROP FUNCTION IF EXISTS queryChildrenAreaInfo1;
DELIMITER;;
CREATE FUNCTION queryChildrenAreaInfo1(areaId INT)
RETURNS VARCHAR(4000)
BEGIN
DECLARE sTemp VARCHAR(4000);
DECLARE sTempChd VARCHAR(4000);
SET sTemp='$';
SET sTempChd = CAST(areaId AS CHAR);
SET sTemp = CONCAT(sTemp,',',sTempChd);
SELECT parentId INTO sTempChd FROM t_areainfo WHERE id = sTempChd;
WHILE sTempChd <> 0 DO
SET sTemp = CONCAT(sTemp,',',sTempChd);
SELECT parentId INTO sTempChd FROM t_areainfo WHERE id = sTempChd;
END WHILE;
RETURN sTemp;
END
;;
DELIMITER ;

6、調(diào)用方式

查詢id為"7"的節(jié)點(diǎn)的所有上級(jí)節(jié)點(diǎn):

SELECT * from t_areainfo where FIND_IN_SET(id,queryChildrenAreaInfo1(7));

mysql怎樣遞歸查詢所有數(shù)據(jù)

以上是mysql怎樣遞歸查詢所有數(shù)據(jù)的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!


網(wǎng)站欄目:mysql怎樣遞歸查詢所有數(shù)據(jù)
分享URL:http://weahome.cn/article/podogi.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部