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