《MysqL学习MysqL递归查询实例解析》要点:
本文介绍了MysqL学习MysqL递归查询实例解析,希望对您有用。如果有疑问,可以联系我们。
MysqL实例办法1,MysqL递归查询.
Navicat MysqL Data Transfer
Source Server : MysqL_demo3
Source Server Version : 50521
Source Host : localhost:3306
Source Database : test
Target Server Type : MysqL
Target Server Version : 50521
file EnCoding : 65001
Date: 2012-09-02 21:16:03
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- table structure for `treenodes`
-- ----------------------------
DROP table IF EXISTS `treenodes`;
CREATE table `treenodes` (
`ID` int(11) NOT NulL,
`nodename` varchar(20) DEFAulT NulL,
`pID` int(11) DEFAulT NulL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAulT CHARSET=latin1;
-- ----------------------------
-- Records of treenodes
-- ----------------------------
INSERT INTO `treenodes` VALUES ('1','A','0');
INSERT INTO `treenodes` VALUES ('2','B','1');
INSERT INTO `treenodes` VALUES ('3','C','1');
INSERT INTO `treenodes` VALUES ('4','D','2');
INSERT INTO `treenodes` VALUES ('5','E','2');
INSERT INTO `treenodes` VALUES ('6','F','3');
INSERT INTO `treenodes` VALUES ('7','G','6');
INSERT INTO `treenodes` VALUES ('8','H','0');
INSERT INTO `treenodes` VALUES ('9','I','8');
INSERT INTO `treenodes` VALUES ('10','J','8');
INSERT INTO `treenodes` VALUES ('11','K','8');
INSERT INTO `treenodes` VALUES ('12','L','9');
INSERT INTO `treenodes` VALUES ('13','M','9');
INSERT INTO `treenodes` VALUES ('14','N','12');
INSERT INTO `treenodes` VALUES ('15','O','12');
INSERT INTO `treenodes` VALUES ('16','P','15');
INSERT INTO `treenodes` VALUES ('17','Q','15');
MysqL实例上边是sql脚本,在执行select * 之后显示的结果集如下:
+----+----------+------+
| ID | nodename | pID |
+----+----------+------+
| 1 | A | 0 |
| 2 | B | 1 |
| 3 | C | 1 |
| 4 | D | 2 |
| 5 | E | 2 |
| 6 | F | 3 |
| 7 | G | 6 |
| 8 | H | 0 |
| 9 | I | 8 |
| 10 | J | 8 |
| 11 | K | 8 |
| 12 | L | 9 |
| 13 | M | 9 |
| 14 | N | 12 |
| 15 | O | 12 |
| 16 | P | 15 |
| 17 | Q | 15 |
+----+----------+------+
17 rows in set (0.00 sec)
MysqL实例树形图:
+-- 2:B
| +-- 4:D
| +-- 5:E
+-- 3:C
+-- 6:F
+-- 7:G
8:H
+-- 9:I
| +-- 12:L
| | +--14:N
| | +--15:O
| | +--16:P
| | +--17:Q
| +-- 13:M
+-- 10:J
+-- 11:K
MysqL实例如果给你一个这样的table,让你查询根节点为1下的所有节点记录(注意也包括根节点)?
可能有不少人想到connect by 函数,但是我灰常遗憾的告诉你,咱这儿是MysqL!
解决办法:利用函数来得到所有子节点号.
闲话少续,看我的解决办法
创建一个function getChildLst,得到一个由所有子节点号组成的字符串.
MysqL>
MysqL> CREATE FUNCTION `getChildLst`(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 treeNodes where FIND_IN_SET(pID,stempChd)>0;
END WHILE;
RETURN stemp;
END
;
query OK,0 rows affected (0.00 sec)
MysqL>
MysqL> delimiter ;
MysqL实例
使用我们直接利用find_in_set函数配合这个getChildlst来查找:
+-----------------+
| getChildLst(1) |
+-----------------+
| $,1,2,3,4,5,6,7 |
+-----------------+
1 row in set (0.00 sec)
MysqL> select * from treeNodes
-> where FIND_IN_SET(ID,getChildLst(1));
+----+----------+------+
| ID | nodename | pID |
+----+----------+------+
| 1 | A | 0 |
| 2 | B | 1 |
| 3 | C | 1 |
| 4 | D | 2 |
| 5 | E | 2 |
| 6 | F | 3 |
| 7 | G | 6 |
+----+----------+------+
7 rows in set (0.01 sec)
MysqL> select * from treeNodes
-> where FIND_IN_SET(ID,getChildLst(3));
+----+----------+------+
| ID | nodename | pID |
+----+----------+------+
| 3 | C | 1 |
| 6 | F | 3 |
| 7 | G | 6 |
+----+----------+------+
3 rows in set (0.01 sec)
MysqL实例只要按我的做,百发百中百步穿杨,遇到问题万变不离其宗直接粘贴复制就是.
补充:
还可以做嵌套查询:
select ID from treeNodes where FIND_IN_SET(ID,getChildLst(3))
);
MysqL实例子查询的结果集是:
+--------+
ID
----
3
6
7
+-------+
然后,经过外层查询就是:
ID pID
3 1
6 3
6 6
---------
MysqL递归查询替代函数实例
MysqL递归查询树形叶子
Oracle递归查询树形结构
MysqL 递归查询当前节点子节点
MysqL递归查询实现办法
Oracle递归查询SQL语句分享
sql2005递归查询的例子
sql递归查询(with cte实现)
sql 递归查询的代码(图文)
内存溢出PHP培训学院每天发布《MysqL学习MysqL递归查询实例解析》等实战技能,PHP、MysqL、liNUX、APP、Js,CSS全面培养人才。
总结以上是内存溢出为你收集整理的Mysql学习mysql递归查询实例解析全部内容,希望文章能够帮你解决Mysql学习mysql递归查询实例解析所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)