栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【MySQL常见面试题】MySQL树结构数据查询

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【MySQL常见面试题】MySQL树结构数据查询

文章目录
  • 数据准备、说明
    • 1. 查询树的根节点
    • 2. 查询树的叶子节点
    • 3. 查询既不是叶子也不是根的节点
  • 组合在一起查询
  • case的查询方式

数据准备、说明

新建一张组织结构表,字段如下

mysql> desc t_org;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | int(11)      | NO   | PRI | NULL    |       |
| p_id  | int(11)      | YES  |     | NULL    |       |
| name  | varchar(255) | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+

插入一些数据

mysql> insert into t_org (id, p_id, name) values (1,null,'一级机构'),(2,1,'二级机构1-1'),(3,1,'二级机构1-2'),(4,2,'三级机构2-1'),(5,3,'三级机构3-1');
Query OK, 5 rows affected (0.03 sec)
Records: 5  Duplicates: 0  Warnings: 0
mysql> select * from t_org;
+----+------+-------------+
| id | p_id | name        |
+----+------+-------------+
|  1 | NULL | 一级机构    |
|  2 |    1 | 二级机构1-1 |
|  3 |    1 | 二级机构1-2 |
|  4 |    2 | 三级机构2-1 |
|  5 |    3 | 三级机构3-1 |
+----+------+-------------+
5 rows in set (0.06 sec)
1. 查询树的根节点

也就是没有父节点的节点

mysql> select * from t_org where p_id is null;
+----+------+----------+
| id | p_id | name     |
+----+------+----------+
|  1 | NULL | 一级机构 |
+----+------+----------+
1 row in set (0.03 sec)
2. 查询树的叶子节点

也就是没有孩子、且有父节点的节点

mysql> select * from t_org where id not in (select distinct p_id from t_org where p_id is not null) and p_id is not null;
+----+------+-------------+
| id | p_id | name        |
+----+------+-------------+
|  4 |    2 | 三级机构2-1 |
|  5 |    3 | 三级机构3-1 |
+----+------+-------------+
2 rows in set (0.05 sec)
3. 查询既不是叶子也不是根的节点

也就是有孩子、且有父节点的节点

mysql> select * from t_org where id in (select distinct p_id from t_org where p_id is not null) and p_id is not null;
+----+------+-------------+
| id | p_id | name        |
+----+------+-------------+
|  2 |    1 | 二级机构1-1 |
|  3 |    1 | 二级机构1-2 |
+----+------+-------------+
2 rows in set (0.05 sec)
组合在一起查询
mysql> select id, name, 'root' as type from t_org where p_id is null union all select id, name, 'inner' as type from t_org where id in (select distinct p_id from t_org where p_id is not null) and p_id is not null union all select id, name, 'leaf' as type from t_org where id not in (select distinct p_id from t_org where p_id is not null) and p_id is not null;
+----+-------------+-------+
| id | name        | type  |
+----+-------------+-------+
|  1 | 一级机构    | root  |
|  2 | 二级机构1-1 | inner |
|  3 | 二级机构1-2 | inner |
|  4 | 三级机构2-1 | leaf  |
|  5 | 三级机构3-1 | leaf  |
+----+-------------+-------+
5 rows in set (0.07 sec)
case的查询方式
mysql> select id, name, case
    -> when id in (select id from t_org where p_id is null) then 'root'
    -> when id in (select p_id from t_org where p_id is not null) then 'inner'
    -> else 'leaf' end as 'type' from t_org;
+----+-------------+-------+
| id | name        | type  |
+----+-------------+-------+
|  1 | 一级机构    | root  |
|  2 | 二级机构1-1 | inner |
|  3 | 二级机构1-2 | inner |
|  4 | 三级机构2-1 | leaf  |
|  5 | 三级机构3-1 | leaf  |
+----+-------------+-------+
5 rows in set (0.04 sec)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/870470.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号