一个嵌套语句!!

白衣染霜陈 2005-03-09 11:36:34
表class里边是“类别管理表”,里边有classid,classname,isbig,belongto等等字段
其中classname表明该类的名字,isbig表时是否是大类(0为大类,1为小类),belongto则表明如果是小类,它属于的大类的classid号。

比如,大类“电脑”的classid是5,小类“音箱”和“鼠标”的belongto则是5

现在要求,将所有类别取出,但按照
大类1
所属小类1
所属小类2
大类2
所属小类1
所属小类2
所属小类3
这样的秩序取出,请问怎么做到。要求用一句SQL语句!!
谢谢!!
...全文
242 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
白衣染霜陈 2005-03-09
  • 打赏
  • 举报
回复
select
大类 = (case isbig when 0 then classname end),
小类 = (case isbig when 1 then classname end)
order by
case isbig when 0 then classid else belongto end,
case isbig when 0 then 0 else classid end

这句话是什么意思???
woodcord 2005-03-09
  • 打赏
  • 举报
回复
再顶!
631799 2005-03-09
  • 打赏
  • 举报
回复
参考这个:
create table tb(id int identity(1,1) not null constraint PK_tb primary key clustered
,pid int,name varchar(20))
insert into tb
select 0,'中国'
union all select 0,'美国'
union all select 0,'加拿大'
union all select 1,'北京'
union all select 1,'上海'
union all select 1,'江苏'
union all select 6,'苏州'
union all select 7,'常熟'
union all select 6,'南京'
union all select 6,'无锡'
union all select 2,'纽约'
union all select 2,'旧金山'
go

欢迎大家提出更多,更好的这方面的方法.




/*--数据处理--*/

/*-- 一个重要的函数,很多处理的地方都会用到 --*/
--自定义函数--获取编码累计
create function f_getmergid(@id int)
returns varchar(8000)
as
begin
declare @re varchar(8000),@pid int

--为了数字排序正常,需要统一编码宽度
declare @idlen int,@idheader varchar(20)
select @idlen=max(len(id))
,@idheader=space(@idlen)
from tb

--得到编码累计
set @re=right(@idheader+cast(@id as varchar),@idlen)
select @pid=pid from tb where id=@id
while @@rowcount>0
select @re=right(@idheader+cast(@pid as varchar),@idlen)+','+@re
,@pid=pid from tb where id=@pid
return(@re)
end
go

--分级显示--纵向
select * from tb order by dbo.f_getmergid(id)
go
pbsql 2005-03-09
  • 打赏
  • 举报
回复
一句实现不了的,树结构遍历,参考本帖:
http://community.csdn.net/Expert/topic/3831/3831460.xml?temp=.7624933
pp_key 2005-03-09
  • 打赏
  • 举报
回复
select a.classname,b.classname from (select * from class where isbig='0') a inner join (select * from class where isbig='1') b on a.classid=b.belongto
order by a.classname,b.classname
子陌红尘 2005-03-09
  • 打赏
  • 举报
回复
select
大类 = (case isbig when 0 then classname end),
小类 = (case isbig when 1 then classname end)
order by
case isbig when 0 then classid else belongto end,
case isbig when 0 then 0 else classid end
子陌红尘 2005-03-09
  • 打赏
  • 举报
回复
select
分类 = (case isbig when 0 then '大类:'+classname else '小类:'+classname end)
order by
case isbig when 0 then classid else belongto end,
case isbig when 0 then 0 else classid end
白衣染霜陈 2005-03-09
  • 打赏
  • 举报
回复
如果将这些类添加到<select>列表里边,要求必须选择小类,那该如何输出??
白衣染霜陈 2005-03-09
  • 打赏
  • 举报
回复
哦, 如果是用在ASP程序中,那怎么写语句??
子陌红尘 2005-03-09
  • 打赏
  • 举报
回复
select
大类 = (case isbig when 0 then classname end),
小类 = (case isbig when 1 then classname end)
order by
case isbig when 0 then classid else belongto end,
case isbig when 0 then 0 else classid end

这句话是什么意思???
-------------------------------------------------------
select
--当class为大类时,输出classname,否则,输出NULL
大类 = (case isbig when 0 then classname end),

--当class为小类时,输出classname,否则,输出NULL
小类 = (case isbig when 1 then classname end)
order by

--按照大类的classid和小类的belongto字段排序
case isbig when 0 then classid else belongto end,

--当class为大类时,设置该值为0,否则使用小类的classid,
--以保障按照上述方式排序的同时,始终将大类的classname在
--本分组的第一行输出
case isbig when 0 then 0 else classid end
lishengyu 2005-03-09
  • 打赏
  • 举报
回复
create table tb(id int identity(1,1) not null constraint PK_tb primary key clustered
,ParentID int,name varchar(20))
insert into tb
select 0,'中国'
union all select 0,'美国'
union all select 0,'加拿大'
union all select 1,'北京'
union all select 1,'上海'
union all select 1,'江苏'
union all select 6,'苏州'
union all select 7,'常熟'
union all select 6,'南京'
union all select 6,'无锡'
union all select 2,'纽约'
union all select 2,'旧金山'

go
declare @i int
set @i=1
create table #tmp(id int,px varchar(1000),parentid int)
insert into #tmp select [id], '', parentID from tb order by id

update #tmp set px=cast(@i+10000000 as varchar) ,@i=@i+1
from #tmp where parentid=0

while @@rowcount>0
update b set b.px=a.px+cast(@i+10000000 as varchar),@i=@i+1
from #tmp a join #tmp b
on a.id=b.parentID and a.px<>'' and b.px=''

select b.*,space(len(a.px)/2)+b.name 缩进的 from
#tmp a join tb b on a.id=b.id order by a.px
drop table #tmp
drop table tb

id ParentID name 缩进的
----------- ----------- -------------------- ----------------------
1 0 中国 中国
4 1 北京 北京
5 1 上海 上海
6 1 江苏 江苏
7 6 苏州 苏州
8 7 常熟 常熟
9 6 南京 南京
10 6 无锡 无锡
2 0 美国 美国
11 2 纽约 纽约
12 2 旧金山 旧金山
3 0 加拿大 加拿大

(所影响的行数为 12 行)

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧