我只有一個表 「tbl_test」PL SQL查詢recuresive循環
其中有表提起低於
tbl_test table
trx_id | proj_num | parent_num|
1 | 14 | 0 |
2 | 14 | 1 |
3 | 14 | 2 |
4 | 14 | 0 |
5 | 14 | 3 |
6 | 15 | 0 |
結果,我想給出的是:當trx_id值5被取出
這是一個父母子女關係。所以,
trx_id -> parent_num
5 -> 3
3 -> 2
2 -> 1
這意味着輸出值:
3
2
1
讓所有父鏈
查詢我所用:
SELECT * FROM (
WITH RECURSIVE tree_data(project_num, task_num, parent_task_num) AS(
SELECT project_num, task_num, parent_task_num
FROM tb_task
WHERE project_num = 14 and task_num = 5
UNION ALL
SELECT child.project_num, child.task_num, child.parent_task_num
FROM tree_data parent Join tb_task child
ON parent.task_num = child.task_num AND parent.task_num = child.parent_task_num
)
SELECT project_num, task_num, parent_task_num
FROM tree_data
) AS tree_list ;
任何人可以幫助我嗎?
看看遞歸的CTE('WITH RECURSIVE'查詢)。 – 2013-03-14 12:24:04
檢查但令人困惑... – 2013-03-14 12:28:57
Mysql不支持WITH,所以最好試試這個, http://stackoverflow.com/questions/1382573/how-do-you-use-the-with-clause -in-mysql – Balamurugan 2013-03-14 12:29:16