首页 数据库 mysql教程 sql多级分类汇总实现介绍

sql多级分类汇总实现介绍

Jun 07, 2016 pm 05:49 PM
多级分类

本文章介绍了关于sql多级分类汇总实现方法及数据结构,有碰到问题的同学可参考一下。

据库结构如下
类别表
分类id 上级分类id 分类名称 分类级别 排序值

 代码如下 复制代码
id parentid categoryname categorylevel ordering
 代码如下 复制代码
id parentid categoryname categorylevel ordering
1   null      c1            1           1
2    1        c11           2           1
3    1        c12           2           2
4    1        c13           2           3
5    1        c14           2           4
6    2        c111          3           1
7    2        c112          3           2
1   null      c1            1           1

2    1        c11           2           1
3    1        c12           2           2

4    1        c13           2           3
 代码如下 复制代码
id categoryid .........
1    1       ........
2    4       ........
3    5       ........
5    1        c14           2           4

6    2        c111          3           1
7    2        c112          3           2


然后 内容表是 内容id 类别id .........

 代码如下 复制代码
id categoryid .........
1    1       ........ 2    4       ........
 代码如下 复制代码
with area as(
select *,id px,cast(id as nvarchar(4000)) px2 from region where parentid=0
union all
select a.*,b.px,b.px2 ltrim(a.region_id) from region a join area b on a.parentid=b.id
)select * from area px,px2
3    5       ........


这样处理的弊端是:如果数据量大,子分类很多,达到4级以上,这方法处理极端占用连接池

对性能影响很大。
 代码如下 复制代码
id title parentid
1 广东省 0
2 广州 1
3 白云区 2
4 深圳 1
5 湖南省 0
6 长沙 5
7 株洲 5
如果用SQL下面的CTE递归处理的话,一次性就能把结果给查询出来,而且性能很不错 比用程序处理(数据量很大的情况),临时表性能更好,更方便 
 代码如下 复制代码
with area as( select *,id px,cast(id as nvarchar(4000)) px2 from region where parentid=0 union all select a.*,b.px,b.px2 ltrim(a.region_id) from region a join area b on a.parentid=b.id )select * from area px,px2
可以查询出结果—-所有分类及相应分类下子分类
 代码如下 复制代码
id title parentid 1 广东省 0 2 广州 1 3 白云区 2 4 深圳 1 5 湖南省 0 6 长沙 5 7 株洲 5
 代码如下 复制代码


with area as(
select * from region where parentid=1
union all
select a.* from region a join area b on a.parentid=b.id
)select * from area

可以查询出结果—-指定分类及相应分类下子分类
id title parentid
1 广东省 0
2 广州 1
3 白云区 2


实现程序

 代码如下 复制代码
 代码如下 复制代码

/*
标题:查询指定节点及其所有子节点的函数
作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-05-12
地点:广东深圳
*/

create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
insert into tb values('001' , null  , '广东省')
insert into tb values('002' , '001' , '广州市')
insert into tb values('003' , '001' , '深圳市')
insert into tb values('004' , '002' , '天河区')
insert into tb values('005' , '003' , '罗湖区')
insert into tb values('006' , '003' , '福田区')
insert into tb values('007' , '003' , '宝安区')
insert into tb values('008' , '007' , '西乡镇')
insert into tb values('009' , '007' , '龙华镇')
insert into tb values('010' , '007' , '松岗镇')
go

--查询指定节点及其所有子节点的函数
create f_cid(@ID varchar(3)) returns @t_level table(id varchar(3) , level int)
as
begin
  declare @level int
  set @level = 1
  insert into @t_level select @id , @level
  while @@ROWCOUNT > 0
  begin
    set @level = @level 1
    insert into @t_level select a.id , @level
    from tb a , @t_Level b
    where a.pid = b.id and b.level = @level - 1
  end
  return
end
go

--调用函数查询001(广东省)及其所有子节点
select a.* from tb a , f_cid('001') b where a.id = b.id order by a.id
/*
id   pid  name      
---- ---- ----------
001  NULL 广东省
002  001  广州市
003  001  深圳市
004  002  天河区
005  003  罗湖区
006  003  福田区
007  003  宝安区
008  007  西乡镇
009  007  龙华镇
010  007  松岗镇

(所影响的行数为 10 行)
*/

--调用函数查询002(广州市)及其所有子节点
select a.* from tb a , f_cid('002') b where a.id = b.id order by a.id
/*
id   pid  name      
---- ---- ----------
002  001  广州市
004  002  天河区

(所影响的行数为 2 行)
*/

--调用函数查询003(深圳市)及其所有子节点
select a.* from tb a , f_cid('003') b where a.id = b.id order by a.id
/*
id   pid  name      
---- ---- ----------
003  001  深圳市
005  003  罗湖区
006  003  福田区
007  003  宝安区
008  007  西乡镇
009  007  龙华镇
010  007  松岗镇

(所影响的行数为 7 行)
*/

drop table tb
drop function f_cid

 

/* 标题:查询指定节点及其所有子节点的函数 作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开) 时间:2008-05-12 地点:广东深圳 */ create table tb(id varchar(3) , pid varchar(3) , name varchar(10)) insert into tb values('001' , null  , '广东省') insert into tb values('002' , '001' , '广州市') insert into tb values('003' , '001' , '深圳市') insert into tb values('004' , '002' , '天河区') insert into tb values('005' , '003' , '罗湖区') insert into tb values('006' , '003' , '福田区') insert into tb values('007' , '003' , '宝安区') insert into tb values('008' , '007' , '西乡镇') insert into tb values('009' , '007' , '龙华镇') insert into tb values('010' , '007' , '松岗镇') go --查询指定节点及其所有子节点的函数 create f_cid(@ID varchar(3)) returns @t_level table(id varchar(3) , level int) as begin   declare @level int   set @level = 1   insert into @t_level select @id , @level   while @@ROWCOUNT > 0   begin     set @level = @level 1     insert into @t_level select a.id , @level     from tb a , @t_Level b     where a.pid = b.id and b.level = @level - 1   end   return end go --调用函数查询001(广东省)及其所有子节点 select a.* from tb a , f_cid('001') b where a.id = b.id order by a.id /* id   pid  name       ---- ---- ---------- 001  NULL 广东省 002  001  广州市 003  001  深圳市 004  002  天河区 005  003  罗湖区 006  003  福田区 007  003  宝安区 008  007  西乡镇 009  007  龙华镇 010  007  松岗镇 (所影响的行数为 10 行) */ --调用函数查询002(广州市)及其所有子节点 select a.* from tb a , f_cid('002') b where a.id = b.id order by a.id /* id   pid  name       ---- ---- ---------- 002  001  广州市 004  002  天河区 (所影响的行数为 2 行) */ --调用函数查询003(深圳市)及其所有子节点 select a.* from tb a , f_cid('003') b where a.id = b.id order by a.id /* id   pid  name       ---- ---- ---------- 003  001  深圳市 005  003  罗湖区 006  003  福田区 007  003  宝安区 008  007  西乡镇 009  007  龙华镇 010  007  松岗镇 (所影响的行数为 7 行) */ drop table tb drop function f_cid  

实例2

 

 代码如下 复制代码

t1
id     parentid
m    a
n    a
e    m
f    m
x    f
y    f
z    b

t2
row    id      amount
1    a    13.00
2    b    20.00
3    e    20.00
4    f    20.00
5    x    20.00
6    y    20.00
7    z    20.00
8    e    12.00
9    x    11.00
10    f    13.00

如何得出如下结果:

row     id      amount
7    x    20.00
11    x    11.00
    x小计    31.00
8    y    20.00
    y小计    20.00
6    f    20.00
12    f    13.00
    f小计    84.00
5    e    20.00
10    e    12.00
    e小计    32.00
3    m    14.00
    m小计    130.00
4    n    13.00
    n小计    13.00
1    a    13.00
    a小计    156.00
9    z    20.00
    z小计    20.00
2    b    20.00
    b小计    40.00
    总计    196.00

实现程序

-- 示例数据
 创建表 t1(
  id 字符(1),
  父代 char(1)
 );
 插入 t1
 选择 'm', 'a' UNION ALL
 选择 'n', 'a' UNION ALL
 选择 'e', 'm' UNION ALL
 选择 'f', 'm' UNION ALL
 选择 'x', 'f' UNION ALL
 选择 'y', 'f' UNION ALL
 选择“z”、“b”;
 
 创建表 t2(
  行整数,
  id 字符(1),
  金额小数(10, 2)
 );
 插入 t2
 选择 '1', 'a', '13.00' UNION ALL
 选择 '2'、'b'、'20.00' UNION ALL
 选择 '3'、'e'、'20.00' UNION ALL
 选择 '4'、'f'、'20.00' UNION ALL
 选择“5”、“x”、“20.00”联合所有
 选择 '6'、'y'、'20.00' UNION ALL
 选择 '7', 'z', '20.00' UNION ALL
 选择 '8'、'e'、'12.00' UNION ALL
 选择 '9'、'x'、'11.00' 联合所有
 选择“10”、“f”、“13.00”;
 去
 
 -- 统计
 -- 逐级汇总
 声明 @l int
 设置@l=1
 
 选择
  A.[id],
  [pid] = A.parentid,
  [sumnum] = SUM(B.金额),
     级别=案例
         当存在时(select * from t1 where Parentid=a.[id])
         然后 @l-1 else @l 结束
 进入 [#]
 来自 t1 A
  左连接 t2 B
   ON A.id = B.id
 GROUP BY A.id、A.parentid;
 
 if @@row/42852.htm target=_blank >count>0
     在 [#]([id],[pid])
上创建索引 IDX_#_id_pid  否则
     设置@l=999
 
 而@@rowcount>0 或@l=1
 开始
     设置@l=@l 1
     更新集合 level=@l,[sumnum]=isnull(a.[sumnum],0) isnull(b.[sumnum],0)
     来自 [#] a,(
         选择 aa.pid,[sumnum]=sum(aa.[sumnum])
         来自 [#] aa,(
             从 [#]
中选择不同的 [pid]              其中 level=@l-1
         )bb 其中 aa.[pid]=bb.[pid]
             并且不存在(
                 SELECT * FROM [#] WHERE [PID]=aa.[PID] AND [Level]=0)
         按 aa 分组。[PID]
         有 sum(aa.level=0 时则 1 else 0 结束)=0
     )b 其中 a.[id]=b.[pid]
 结束
 
 --最终结果
 选择
  行=案例
    当 GROUPING(A.row) = 0 时,则 RTRIM(A.row)
    其他 N''
   结束,
  id = 案例
    当 GROUPING(A.row) = 0 时,则 A.id
    WHEN GROUPING(A.id) = 0 THEN A.id '小计'
    ELSE N'总共'
   结束,
  金额 = CASE
     当 GROUPING(A.row) = 0 THEN SUM(A.amount)
     WHEN GROUPING(A.id) = 0 THEN ISNULL((SELECT SUM(B.sumnum) FROM # B WHERE A.id = B.id), SUM(A.amount))
     ELSE SUM(A.金额)
    结束
 来自 t2 A
 GROUP BY A.id,A.row AND ROLLUP;
 删除表 [#]
 去
 
 删除表 t1、t2;
 
 /*-- 结果
 行          id                                     金额
 ------------ ----- --------------------------------- ------
 1            a                                       13.00
              小计                                    13.00
 2            b                                       20.00
              b小计                                     20.00
 3            e                                       20.00
 8            e                                       12.00
              e小计                                     32.00
 4            f                                       20.00
 10           f                                       13.00
              f小计                                     84.00
 5            x                                       20.00
 9            x                                       11.00
              x小计                                     31.00
 6            y                                       20.00
              y小计                                     20.00
 7            z                                       20.00
              z小计                                     20.00
              总计                                     169.00
 
 (18 行受影响)
 --*/ 

性能分析:
对于一个3500条地区记录的数据表,其中有省,市,县3级
查询用时要1秒,视觉上感觉有点点慢,但不影响
数据量不大的分类,使用绝对无压力

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

如何使用Alter Table语句在MySQL中更改表? 如何使用Alter Table语句在MySQL中更改表? Mar 19, 2025 pm 03:51 PM

本文讨论了使用MySQL的Alter Table语句修改表,包括添加/删除列,重命名表/列以及更改列数据类型。

说明InnoDB全文搜索功能。 说明InnoDB全文搜索功能。 Apr 02, 2025 pm 06:09 PM

InnoDB的全文搜索功能非常强大,能够显着提高数据库查询效率和处理大量文本数据的能力。 1)InnoDB通过倒排索引实现全文搜索,支持基本和高级搜索查询。 2)使用MATCH和AGAINST关键字进行搜索,支持布尔模式和短语搜索。 3)优化方法包括使用分词技术、定期重建索引和调整缓存大小,以提升性能和准确性。

如何为MySQL连接配置SSL/TLS加密? 如何为MySQL连接配置SSL/TLS加密? Mar 18, 2025 pm 12:01 PM

文章讨论了为MySQL配置SSL/TLS加密,包括证书生成和验证。主要问题是使用自签名证书的安全含义。[角色计数:159]

哪些流行的MySQL GUI工具(例如MySQL Workbench,PhpMyAdmin)是什么? 哪些流行的MySQL GUI工具(例如MySQL Workbench,PhpMyAdmin)是什么? Mar 21, 2025 pm 06:28 PM

文章讨论了流行的MySQL GUI工具,例如MySQL Workbench和PhpMyAdmin,比较了它们对初学者和高级用户的功能和适合性。[159个字符]

您如何处理MySQL中的大型数据集? 您如何处理MySQL中的大型数据集? Mar 21, 2025 pm 12:15 PM

文章讨论了处理MySQL中大型数据集的策略,包括分区,碎片,索引和查询优化。

InnoDB中的聚类索引和非簇索引(次级索引)之间的差异。 InnoDB中的聚类索引和非簇索引(次级索引)之间的差异。 Apr 02, 2025 pm 06:25 PM

聚集索引和非聚集索引的区别在于:1.聚集索引将数据行存储在索引结构中,适合按主键查询和范围查询。2.非聚集索引存储索引键值和数据行的指针,适用于非主键列查询。

如何使用Drop Table语句将表放入MySQL中? 如何使用Drop Table语句将表放入MySQL中? Mar 19, 2025 pm 03:52 PM

本文讨论了使用Drop Table语句在MySQL中放下表,并强调了预防措施和风险。它强调,没有备份,该动作是不可逆转的,详细介绍了恢复方法和潜在的生产环境危害。

如何在JSON列上创建索引? 如何在JSON列上创建索引? Mar 21, 2025 pm 12:13 PM

本文讨论了在PostgreSQL,MySQL和MongoDB等各个数据库中的JSON列上创建索引,以增强查询性能。它解释了索引特定的JSON路径的语法和好处,并列出了支持的数据库系统。

See all articles