无限分级数量查询优化

news/2024/7/3 18:39:32

无限分级的数据查询是个头痛的问题,递归查询类别,再组合成字符串,用 in 来解决子类所有产品的问题,但是这个效率太低,低的让人无法接受,在此,有一个SQL的方法,可让我们提高效率。
-----提取子类的所有类别ID
create function GetChild (@id int)
returns @t table(id int)
as
begin
    insert @t select classid from mproclass where parentid = @id
    while @@rowcount > 0
        insert @t select a.classid from mproclass as a inner join @t as b
        on a.parentid = b.id and a.classid not in(select id from @t)
   return
end
-----提取子类以及自己ID的所有类别ID
create function GetChildAndSelf (@id int)
returns @t table(id int)
as
begin
    insert @t values (@id)
    insert @t select classid from mproclass where parentid = @id
    while @@rowcount > 0
        insert @t select a.classid from mproclass as a inner join @t as b
        on a.parentid = b.id and a.classid not in(select id from @t)
   return
end

以上是最佳方案

查询该类别的产品数据的sql为:
sql = "select * from mProduct as a inner join (select [id] from GetChildAndSelf("+classid+")) as b on a.classid=b.id order by psortid desc,pdate desc";

太爽了!

网上还有其他几种方法,贴出来大家一起学习学习

一、
declare   @table   table(id   int,upperid   int)
insert   into   @table
select   1,           2
union   all   select   3,           2
union   all   select   4,           1
union   all   select   5,           3
declare   @upperid   int  
set   @upperid=2;
with   result(id,upperid)
as
(
select   id,upperid   from   @table   where   upperid=@upperid
union   all
select   a.id,a.upperid   from   @table   a   inner   join   result   b   on   a.upperid=b.id
)
select*from   result
/*
id                     upperid
-----------   -----------
1                       2
3                       2
5                       3
4                       1

(4   row(s)   affected)
*/

二、
Create   table   t(id   int,upperid   int)
insert   into   t
select   1,           2
union   all   select   3,           2
union   all   select   4,           1
union   all   select   5,           3
select   *   from   t
create   function   aa(@upperid   int)
returns   @t   table   (id   int,upperid   int,level   int)
as
begin
declare   @i   int
set   @i=1
insert   into   @t
select   *,@i   from   t   where   upperid=@upperid
while   @@rowcount> 0
begin
set   @i=@i+1
insert   into   @t
select   a.*,@i   from   t   a   left   join   @t   b   on   a.upperid=b.id
where   b.level=@i-1
end
return
end

select   *   from   dbo.aa(1)

id                     upperid           level              
-----------   -----------   -----------  
4                       1                       1

(所影响的行数为   1   行)

select   *   from   dbo.aa(2)

id                     upperid           level              
-----------   -----------   -----------  
1                       2                       1
3                       2                       1
4                       1                       2
5                       3                       2


三、
----创建测试数据
if   object_id( 'tbTest ')   is   not   null
drop   table   tbTest
if   object_id( 'spGetChildren ')   is   not   null
drop   proc   spGetChildren
GO
create   table   tbTest(id   int,     upperid   int)
insert   tbTest
select   1,           2   union   all
select   3,           2   union   all
select   4,           1   union   all
select   5,           3
GO
----创建存储过程
create   proc   spGetChildren   @id   int
as
        declare   @t   table(id   int)
        insert   @t   select   id   from   tbTest   where   upperid   =   @id
        while   @@rowcount   >   0
                insert   @t   select   a.id   from   tbTest   as   a   inner   join   @t   as   b
                on   a.upperid   =   b.id   and   a.id   not   in(select   id   from   @t)
        select   *   from   @t
GO

----执行存储过程
declare   @upperid   int
set   @upperid   =   2
EXEC   spGetChildren   @upperid

----清除测试环境
drop   proc   spGetChildren
drop   table   tbTest

/*结果
id                    
-----------  
1
3
4
5
*/


http://www.niftyadmin.cn/n/3693606.html

相关文章

Asp.Net Web项目打包

1、打开你的项目&#xff0c;在<解决方案管理器>中用鼠标右击你的<解决方案>&#xff0c;选择<添加>-<新建项目>。2、<添加新项目>对话框中选择<安装和部署项目>-<web安装项目>。&#xff08;注意&#xff1a;<web安装项目>的…

刷题总结9.6

在一个表单中&#xff0c;如果想要给输入框添加一个输入验证&#xff0c;可以用下面的哪个事件实现&#xff1f; change可以用作输入验证的事件&#xff0c;change(fn)只是一种实现方式&#xff0c;还有其他实现方式&#xff1a; 原生js的onchange属性&#xff0c; html属性on…

僵尸世界大战 PC 官方 中文版

Win资源分享目录 Win资源分享目录 僵尸世界大战PC官方中文版 免安装未x加密 一款紧张刺激的四人合作第三人称射击游戏 改编自备受好评的同名电影 前几天刚发布的新作&#xff01; 资源分享 僵尸世界大战PC官方中文版 支持原创 码文不易&#xff0c;希望支持&#xff0c;谢谢…

魂斗罗(CONTRA EVOLUTION):进化革命PC版

Win资源分享目录 Win资源分享目录 魂斗罗(CONTRA EVOLUTION)&#xff1a;进化革命PC版 游戏支持双人模式和人机模式&#xff0c;默认99条命&#xff0c;所有枪械全开&#xff0c;游戏中可以自行切换&#xff0c;选择所需要的枪械使用。另外&#xff0c;支持Sony DualShock3和So…

在GRIDVIEW中合并单元格

/// /// 合并GridView列中相同的行 /// /// GridView对象 /// 需要合并的列 public static void GroupRows(GridView GridView1, int cellNum){int i 0, rowSpanNum 1;while (i < GridView1.Rows.Count - 1){GridViewRow gvr GridView1.Rows[i];for (…

专题玩转IIS

http://www.yesky.com/ServerIndex/77125243130347520/20040528/1802271.shtml

『学习资料推荐』百家讲坛中国历史音频珍藏版

资源分享索引 学习资源分享索引 百家讲坛中国历史音频珍藏版 『学习资料推荐』百家讲坛中国历史音频珍藏版 下载地址见文末 支持原创 码文不易&#xff0c;希望支持&#xff0c;谢谢->支持原创 支付宝扫个红包 [外链图片转存失败(img-PfKgflPa-1562573190848)(https://r…

<img> 标签

<img src"/i/eg_tulip.jpg" alt"上海鲜花港 - 郁金香" />img 元素向网页中嵌入一幅图像。 请注意&#xff0c;从技术上讲&#xff0c;<img> 标签并不会在网页中插入图像&#xff0c;而是从网页上链接图像。<img> 标签创建的是被引用图…