mysql递归查询。

来源:互联网 发布:wap小说网站源码 编辑:程序博客网 时间:2024/05/06 05:54

/*
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> select * from treenodes;
+----+----------+------+
| 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)
 

 

1:A
  +-- 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   
 如果给你一个这样的table,让你查询根节点为1下的所有节点记录(注意也包括根节点)?
可能有不少人想到connect by 函数,但是我灰常遗憾的告诉你,咱这儿是mysql!
 
解决方法:利用函数来得到所有子节点号。
 
闲话少续,看我的解决方法
创建一个function getChildLst, 得到一个由所有子节点号组成的字符串。
本文原始链接:http://www.jbxue.com/db/17554.html

 

mysql> delimiter //
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> select getChildLst(1);
+-----------------+
| 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)
 

只要按我的做,百发百中弹无虚发,遇到问题万变不离其宗直接粘贴复制就是。
 
补充:
还可以做嵌套查询:
 

复制代码 代码示例:select id,pid from treeNodes where id in(
     select id from treeNodes where FIND_IN_SET(id, getChildLst(3))
);
 
子查询的结果集是: 
+--------+
id
----
3
6
7
+-------+
然后,经过外层查询就是:
id  pid
3   1
6   3
6   6
---------


本文原始链接:http://www.jbxue.com/db/17554.html

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 三星pin码忘记了怎么办 who缺陷精子率98怎么办 吃桃胶脸过敏了怎么办 安卓8.0无法充值怎么办 刚下高速限行怎么办 西安限号外地车怎么办 深圳下高速限行怎么办 物业断业主的电怎么办 物业要求拆除外晾衣架怎么办 安置费一次给了房子没建好怎么办 虎牙充值不到账怎么办 平安保险交20年怎么办 u盘里东西乱码了怎么办 手机指环支架松了怎么办 运动完放屁很臭怎么办 腾讯柒个我要会员怎么办 海风吹的变黑了怎么办 冻豆角炖不烂糊怎么办 腿被热水烫了怎么办 肉和冰箱冻住了怎么办 解冻的肉臭了怎么办 临时用电电缆线破皮怎么办 阳台按自来水墙面脱落怎么办 墙被暖气熏黑了怎么办 下水管比地砖低怎么办 马桶下水管短了怎么办 下水管比马桶低怎么办? 寻常疣液氮冷冻后该怎么办 寻常疣冷冻后起水泡胀痛怎么办 冷冻后大水泡痒怎么办 19岁的宫颈糜烂怎么办 发面酸了没有碱怎么办 面发时间长酸了怎么办 丽珠兰打完红肿怎么办 美的冰柜冷藏室结冰怎么办 胃下午和晚上痛怎么办 二胡弓毛粗拉外弦触到里弦怎么办 电视成黑白的了怎么办 城市居民无钱入社保医保怎么办 老婆说离婚又找不到怎么办 爱一个人太累了怎么办