查看: 282|回复: 0

[SQLServer] sqlserver实现树形结构递归查询(无限极分类)的方法

发表于 2017-10-8 08:00:01
尚学堂AD

SQL Server 2005开始,我们可以直接通过CTE来支持递归查询,CTE即公用表表达式

百度百科

公用表表达式(CTE),是一个在查询中定义的临时命名结果集将在from子句中使用它。每个CTE仅被定义一次(但在其作用域内可以被引用任意次),并且在该查询生存期间将一直生存。可以使用CTE来执行递归操作。创建的语法是:

  1. with <name of you cte>(<column names>)
  2. as(
  3. <actual query>
  4. )
  5. select * from <name of your cte>
复制代码

1、生成数据

  1. --菜单目录结构表
  2. create table tb_menu(
  3. id int not null, --主键id
  4. title varchar(50), --标题
  5. parent int --parent id
  6. );
  7. --父菜单
  8. insert into tb_menu(id, title, parent) values(1, '父菜单1',null);
  9. insert into tb_menu(id, title, parent) values(2, '父菜单2',null);
  10. insert into tb_menu(id, title, parent) values(3, '父菜单3',null);
  11. insert into tb_menu(id, title, parent) values(4, '父菜单4',null);
  12. insert into tb_menu(id, title, parent) values(5, '父菜单5',null);
  13. --一级菜单
  14. insert into tb_menu(id, title, parent) values(6, '一级菜单6',1);
  15. insert into tb_menu(id, title, parent) values(7, '一级菜单7',1);
  16. insert into tb_menu(id, title, parent) values(8, '一级菜单8',1);
  17. insert into tb_menu(id, title, parent) values(9, '一级菜单9',2);
  18. insert into tb_menu(id, title, parent) values(10, '一级菜单10',2);
  19. insert into tb_menu(id, title, parent) values(11, '一级菜单11',2);
  20. insert into tb_menu(id, title, parent) values(12, '一级菜单12',3);
  21. insert into tb_menu(id, title, parent) values(13, '一级菜单13',3);
  22. insert into tb_menu(id, title, parent) values(14, '一级菜单14',3);
  23. insert into tb_menu(id, title, parent) values(15, '一级菜单15',4);
  24. insert into tb_menu(id, title, parent) values(16, '一级菜单16',4);
  25. insert into tb_menu(id, title, parent) values(17, '一级菜单17',4);
  26. insert into tb_menu(id, title, parent) values(18, '一级菜单18',5);
  27. insert into tb_menu(id, title, parent) values(19, '一级菜单19',5);
  28. insert into tb_menu(id, title, parent) values(20, '一级菜单20',5);
  29. --二级菜单
  30. insert into tb_menu(id, title, parent) values(21, '二级菜单21',6);
  31. insert into tb_menu(id, title, parent) values(22, '二级菜单22',6);
  32. insert into tb_menu(id, title, parent) values(23, '二级菜单23',7);
  33. insert into tb_menu(id, title, parent) values(24, '二级菜单24',7);
  34. insert into tb_menu(id, title, parent) values(25, '二级菜单25',8);
  35. insert into tb_menu(id, title, parent) values(26, '二级菜单26',9);
  36. insert into tb_menu(id, title, parent) values(27, '二级菜单27',10);
  37. insert into tb_menu(id, title, parent) values(28, '二级菜单28',11);
  38. insert into tb_menu(id, title, parent) values(29, '二级菜单29',12);
  39. insert into tb_menu(id, title, parent) values(30, '二级菜单30',13);
  40. insert into tb_menu(id, title, parent) values(31, '二级菜单31',14);
  41. insert into tb_menu(id, title, parent) values(32, '二级菜单32',15);
  42. insert into tb_menu(id, title, parent) values(33, '二级菜单33',16);
  43. insert into tb_menu(id, title, parent) values(34, '二级菜单34',17);
  44. insert into tb_menu(id, title, parent) values(35, '二级菜单35',18);
  45. insert into tb_menu(id, title, parent) values(36, '二级菜单36',19);
  46. insert into tb_menu(id, title, parent) values(37, '二级菜单37',20);
  47. --三级菜单
  48. insert into tb_menu(id, title, parent) values(38, '三级菜单38',21);
  49. insert into tb_menu(id, title, parent) values(39, '三级菜单39',22);
  50. insert into tb_menu(id, title, parent) values(40, '三级菜单40',23);
  51. insert into tb_menu(id, title, parent) values(41, '三级菜单41',24);
  52. insert into tb_menu(id, title, parent) values(42, '三级菜单42',25);
  53. insert into tb_menu(id, title, parent) values(43, '三级菜单43',26);
  54. insert into tb_menu(id, title, parent) values(44, '三级菜单44',27);
  55. insert into tb_menu(id, title, parent) values(45, '三级菜单45',28);
  56. insert into tb_menu(id, title, parent) values(46, '三级菜单46',28);
  57. insert into tb_menu(id, title, parent) values(47, '三级菜单47',29);
  58. insert into tb_menu(id, title, parent) values(48, '三级菜单48',30);
  59. insert into tb_menu(id, title, parent) values(49, '三级菜单49',31);
  60. insert into tb_menu(id, title, parent) values(50, '三级菜单50',31);
  61. commit;
复制代码

2.查找所有上级节点

  1. --查询树状结构某节点(44)的上级所有根节点
  2. with cte_parent(id,title,parent)
  3. as
  4. (
  5. --起始条件
  6. select id,title,parent
  7. from tb_menu
  8. where id = 44 --列出子节点查询条件
  9. union all
  10. --递归条件
  11. select a.id,a.title,a.parent
  12. from tb_menu a
  13. inner join
  14. cte_parent b --执行递归,这里就要理解下了
  15. on a.id=b.parent
  16. )
  17. select * from cte_parent;
复制代码

3.查找下级节点带level

  1. --查询树状结构某节点下的所有子节点(
  2. with cte_child(id,title,parent,level)
  3. as
  4. (
  5. --起始条件
  6. select id,title,parent,0 as level
  7. from tb_menu
  8. where id = 6--列出父节点查询条件
  9. union all
  10. --递归条件
  11. select a.id,a.title,a.parent,b.level+1
  12. from tb_menu a
  13. inner join
  14. cte_child b
  15. on ( a.parent=b.id)
  16. )
  17. select * from cte_child;
复制代码

以上这篇sqlserver实现树形结构递归查询(无限极分类)的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持程序员之家。



回复

使用道具 举报