[英]Mysql Mariadb recursive query
我有下表:
CREATE TABLE IF NOT EXISTS `tbl` (
`id` int(12) NOT NULL,
`name` varchar(50) NOT NULL,
`parentid` int(12) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO `tbl` (`id`, `name`, `parentid`) VALUES
(1, 'categ 1', 0),
(2, 'xcateg 1.1', 1),
(3, 'acateg 1.2', 1),
(4, 'categ 1.2.1', 3),
(5, 'categ 2', 0),
(6, 'categ 2.1', 5);
並對其進行遞歸查詢:
WITH RECURSIVE tree_search (id, name, lvl, parentid) AS (
SELECT id, name, 0, parentid
FROM tbl
WHERE parentid = 0
UNION ALL
SELECT t.id, t.name,
ts.lvl + 1, ts.id
FROM tbl AS t
JOIN tree_search AS ts ON t.parentid = ts.id
)
SELECT * FROM tree_search
ORDER BY parentid, lvl, name;
它可以工作並打印以下結果:
id | name | lvl | parentid |
----+--------------+-------+----------+
1 | categ 1 | 0 | 0 |
5 | categ 2 | 0 | 0 |
3 | acateg 1.2 | 1 | 1 |
2 | xcateg 1.1 | 1 | 1 |
4 | categ 1.2.1 | 2 | 3 |
6 | categ 2.1 | 1 | 5 |
我希望在結果中添加一個額外的列,即“parent_name”。 我該怎么做呢?
編輯添加: https : //stackoverflow.com/a/22376973/2027239這是我用來構建查詢的舊問題的答案
使用 tbl 的內部連接
CREATE TABLE IF NOT EXISTS `tbl` ( `id` int(12) NOT NULL, `name` varchar(50) NOT NULL, `parentid` int(12) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; INSERT INTO `tbl` (`id`, `name`, `parentid`) VALUES (1, 'categ 1', 0), (2, 'xcateg 1.1', 1), (3, 'acateg 1.2', 1), (4, 'categ 1.2.1', 3), (5, 'categ 2', 0), (6, 'categ 2.1', 5);
\n ✓\n\n ✓\n
WITH RECURSIVE tree_search (id, name, lvl, parentid) AS ( SELECT id, name, 0, parentid FROM tbl WHERE parentid = 0 UNION ALL SELECT t.id, t.name, ts.lvl + 1, ts.id FROM tbl AS t JOIN tree_search AS ts ON t.parentid = ts.id ) SELECT t.*, tb.`name` FROM tree_search t inner Join tbl tb ON t.parentid = tb.id ORDER BY parentid, lvl, t.name;
\n身份證 | 姓名 | 等級 | 父母 | 姓名 \n -: | :---------- | --: | -------: | :---------\n 3 | acateg 1.2 | 1 | 1 | 類別 1 \n 2 | xcateg 1.1 | 1 | 1 | 類別 1 \n 4 | 類別 1.2.1 | 2 | 3 | 類別 1.2\n 6 | 類別 2.1 | 1 | 5 | 類別 2 \n
db<> 在這里擺弄
或者使用left join
來獲取所有id
即使是那些沒有父級的。
CREATE TABLE IF NOT EXISTS `tbl` ( `id` int(12) NOT NULL, `name` varchar(50) NOT NULL, `parentid` int(12) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; INSERT INTO `tbl` (`id`, `name`, `parentid`) VALUES (1, 'categ 1', 0), (2, 'xcateg 1.1', 1), (3, 'acateg 1.2', 1), (4, 'categ 1.2.1', 3), (5, 'categ 2', 0), (6, 'categ 2.1', 5);
\n ✓\n\n ✓\n
WITH RECURSIVE tree_search (id, name, lvl, parentid) AS ( SELECT id, name, 0, parentid FROM tbl WHERE parentid = 0 UNION ALL SELECT t.id, t.name, ts.lvl + 1, ts.id FROM tbl AS t JOIN tree_search AS ts ON t.parentid = ts.id ) SELECT t.*, tb.`name` FROM tree_search t LEFT Join tbl tb ON t.parentid = tb.id ORDER BY parentid, lvl, t.name;
\n身份證 | 姓名 | 等級 | 父母 | 姓名 \n -: | :---------- | --: | -------: | :---------\n 1 | 類別 1 | 0 | 0 | 空值 \n 5 | 類別 2 | 0 | 0 | 空值 \n 3 | acateg 1.2 | 1 | 1 | 類別 1 \n 2 | xcateg 1.1 | 1 | 1 | 類別 1 \n 4 | 類別 1.2.1 | 2 | 3 | 類別 1.2\n 6 | 類別 2.1 | 1 | 5 | 類別 2 \n
db<> 在這里擺弄
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.