MYSQL 8-如何确定“节点”是否为';叶节点';在CTE中检索树时
我有以下数据:MYSQL 8-如何确定“节点”是否为';叶节点';在CTE中检索树时,mysql,mysql-8.0,Mysql,Mysql 8.0,我有以下数据: mysql> SELECT id, parent_id, name FROM account_type WHERE account_classification_id = 1 LIMIT 7; +----+-----------+----------------------+ | id | parent_id | name | +----+-----------+----------------------+ | 30 | NULL
mysql> SELECT id, parent_id, name FROM account_type WHERE account_classification_id = 1 LIMIT 7;
+----+-----------+----------------------+
| id | parent_id | name |
+----+-----------+----------------------+
| 30 | NULL | AKTIVA |
| 40 | 30 | Aktiva Lancar |
| 41 | 40 | Kas & Bank |
| 42 | 41 | Kas |
| 43 | 41 | Kas Tunai USD |
| 44 | 41 | Kas Tunai Di Brankas |
| 46 | 41 | Bank |
+----+-----------+----------------------+
7 rows in set (0.00 sec)
因此,对于CTE:
WITH RECURSIVE account_path (root, id, parent_id, name, lvl, `sort`, account_roll_up_id) AS
(
SELECT
id AS root,
id,
parent_id,
`name`, 0 lvl,
`sort`,
account_roll_up_id
FROM account_type
WHERE id IN (30, 213)
UNION ALL
SELECT
ap.root,
at.id,
at.parent_id,
at.name,
(ap.lvl + 1),
at.sort,
at.account_roll_up_id
FROM account_path AS ap
JOIN account_type AS at ON ap.id = at.parent_id
)
SELECT * FROM `account_path`
ORDER BY account_path.sort
;
我需要每一行都有一个布尔值,当我检索所有节点时,它被确定为一个叶子节点,
可能吗?
大概是这样的:
+------------+------+------+-----------+----------------------+------+------+--------------------+
| is_a_leaf | root | id | parent_id | name | lvl | sort| account_roll_up_id |
+------------+------+------+------------+----------------------+------+-----+--------------------+
| FALSE| 30 | 30 | NULL | AKTIVA | 0 | 1 | NULL |
| FALSE| 30 | 40 | 30 | Aktiva Lancar | 1 | 2 | 2 |
| FALSE| 30 | 41 | 40 | Kas & Bank | 2 | 3 | 2 |
| TRUE | 30 | 42 | 41 | Kas | 3 | 4 | 2 |
| TRUE | 30 | 43 | 41 | Kas Tunai USD | 3 | 5 | 2 |
| TRUE | 30 | 44 | 41 | Kas Tunai Di Brankas | 3 | 6 | 2 |
| TRUE | 30 | 46 | 41 | Bank | 3 | 7 | 2 |
+------------+------+------+-----------+----------------------+------+------+--------------------+
解决了,谢谢@Akina
WITH RECURSIVE account_path (is_a_leaf, root, id, parent_id, name, lvl, `sort`, account_roll_up_id) AS
(
SELECT id is_a_leaf,
id AS root,
id,
parent_id,
`name`,
0 lvl,
`sort`,
account_roll_up_id
FROM account_type
WHERE id IN (30, 213)
UNION ALL
SELECT (SELECT EXISTS(
SELECT parent_id
FROM account_type
WHERE parent_id = at.id
)),
ap.root,
at.id,
at.parent_id,
at.name,
(ap.lvl + 1),
at.sort,
at.account_roll_up_id
FROM account_path AS ap
JOIN account_type AS at ON ap.id = at.parent_id
)
SELECT *
FROM `account_path`
ORDER BY account_path.sort
LIMIT 7;
“叶痕”是指没有孩子<代码>选择。。不存在({检索childs的子查询})作为IsLeaf..
WITH RECURSIVE account_path (is_a_leaf, root, id, parent_id, name, lvl, `sort`, account_roll_up_id) AS
(
SELECT id is_a_leaf,
id AS root,
id,
parent_id,
`name`,
0 lvl,
`sort`,
account_roll_up_id
FROM account_type
WHERE id IN (30, 213)
UNION ALL
SELECT (SELECT EXISTS(
SELECT parent_id
FROM account_type
WHERE parent_id = at.id
)),
ap.root,
at.id,
at.parent_id,
at.name,
(ap.lvl + 1),
at.sort,
at.account_roll_up_id
FROM account_path AS ap
JOIN account_type AS at ON ap.id = at.parent_id
)
SELECT *
FROM `account_path`
ORDER BY account_path.sort
LIMIT 7;