在MySQL數據庫中,查詢所有父節點是一項常見的任務。下面是一些示例操作,幫助你了解如何查詢所有父節點。
-- 示例1:查詢指定節點的父節點 SELECT * FROM `table_name` WHERE `id` = '指定節點的id' -- 示例2:查詢所有節點的父節點 SELECT * FROM `table_name` WHERE `is_parent` = '1' -- 示例3:查詢所有節點的父節點,并按照id順序排序 SELECT * FROM `table_name` WHERE `is_parent` = '1' ORDER BY `id` ASC -- 示例4:查詢指定節點的所有父節點 WITH RECURSIVE cte AS ( SELECT * FROM `table_name` WHERE `id` = '指定節點的id' UNION ALL SELECT `table_name`.* FROM `table_name` JOIN cte ON `table_name`.`id` = `cte`.`pid` ) SELECT * FROM cte -- 示例5:查詢所有節點的所有父節點 WITH RECURSIVE cte AS ( SELECT * FROM `table_name` WHERE `is_parent` = '1' UNION ALL SELECT `table_name`.* FROM `table_name` JOIN cte ON `table_name`.`id` = `cte`.`pid` ) SELECT * FROM cte
上一篇css獲得同級