无限分级的数据查询是个头痛的问题,递归查询类别,再组合成字符串,用 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
*/
-----提取子类的所有类别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
*/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述