1、test_b表
/*
Navicat Premium Data Transfer
Source Server : localhost
Source Server Type : MySQL
Source Server Version : 50726
Source Host : localhost
Source Database : jtthink
Target Server Type : MySQL
Target Server Version : 50726
File Encoding : utf-8
Date: 01/01/2020 16:36:29 PM
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for `test_a`
-- ----------------------------
DROP TABLE IF EXISTS `test_a`;
CREATE TABLE `test_a` (
`id` int(11) NOT NULL,
`username` varchar(50) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of `test_a`
-- ----------------------------
BEGIN;
INSERT INTO `test_a` VALUES ('73', '群主'),
('74', '管理员1号'), ('75', '管理员2号'),
('76', '群员1号'), ('77', '群员2号'),
('78', '群员3号'), ('80', '群员4号');
COMMIT;
SET FOREIGN_KEY_CHECKS = 1;
2、test_b表
/*
Navicat Premium Data Transfer
Source Server : localhost
Source Server Type : MySQL
Source Server Version : 50726
Source Host : localhost
Source Database : jtthink
Target Server Type : MySQL
Target Server Version : 50726
File Encoding : utf-8
Date: 01/01/2020 16:36:36 PM
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for `test_b`
-- ----------------------------
DROP TABLE IF EXISTS `test_b`;
CREATE TABLE `test_b` (
`ancestor_id` int(11) NOT NULL DEFAULT '0',
`level` int(11) NOT NULL DEFAULT '0',
`user_id` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`ancestor_id`,`level`,`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of `test_b`
-- ----------------------------
BEGIN;
INSERT INTO `test_b` VALUES
('73', '0', '73'), ('73', '1', '74'),
('73', '1', '75'), ('73', '2', '76'),
('73', '2', '77'), ('73', '2', '78'),
('73', '3', '80'), ('74', '0', '74'),
('74', '1', '76'), ('74', '1', '77'),
('74', '2', '80'), ('75', '0', '75'),
('75', '1', '78'), ('76', '0', '76'),
('77', '0', '77'), ('77', '1', '80'),
('78', '0', '78'), ('80', '0', '80');
COMMIT;
SET FOREIGN_KEY_CHECKS = 1;
3、查询出74的所有下级
select a1.* from test_a a1, test_a a2, test_b b where a2.id = 74 and b.ancestor_id = a2.id and a1.id = b.user_id
-- 自己写的
select a.* from test_b b join test_a a on a.id = b.user_id and b.ancestor_id = 74 and level <> 0;
4、查询74的直属下级
select a1.* from test_a a1, test_a a2, test_b b where a2.id = 74 and b.`level` = 1 and b.ancestor_id = a2.id and a1.id = b.user_id
-- 自己写的
select a.* from test_b b join test_a a on a.id = b.user_id and b.ancestor_id = 74 and level = 1;
5、查询74的上级
select a2.* from test_a a1, test_a a2, test_b b where a1.id = 74 and b.`level` = 1 and b.user_id = a1.id and a2.id = b.ancestor_id
-- 自己写的
select a.* from test_b b join test_a a on a.id = b.ancestor_id and b.user_id = 74 and level = 1;
6、查询80的所有上级节点
select a2.* from test_a a1, test_a a2, test_b b where a1.id = 80 and b.`level` > 0 and b.user_id = a1.id and a2.id = b.ancestor_id
--自己写的
select a.* from test_b b join test_a a on a.id = b.ancestor_id and b.user_id = 80 and level > 0;
7、插入新节点
- 插入新节点分两步
- 插入自己的
- 插入上级的
插入 80的新节点
INSERT INTO test_b(ancestor_id,level,user_id) VALUES(80,0,80)
INSERT INTO test_b(ancestor_id,level,user_id) (SELECT ancestor_id,level + 1, 80 FROM test_b WHERE user_id=77)
8、查询80是否是74的下级
select count(1) from test_b where user_id = 80 and b.ancestor_id = 74