日期:2014-05-18  浏览次数:20468 次

请教大家一个关于SQL查询的问题
我现在有一张表,里面有如下数据

CommClsssID     CommCLassName
01                       卷烟
02                       白酒
03                       瓜子
01001                 云南卷烟厂
01002                 昆明卷烟厂
02001                 四川郎酒
02002                 贵州茅台
03001                 恰恰瓜子
03002                 大好大瓜子


我现在有一个需求就是需要里面   CommClassID   为01   02   03   的数据   作为程序里TREEVIEW的父节点       余下的分别做   他们的子节点,我该如何写SQL语句     获得CLASSID   为01   02   03   的3条数据???   谢谢大家~~~~

------解决方案--------------------
http://www.v2studio.cn/blog/gaojier/article.asp?id=29
树型菜单的应用!借鉴一下吧!
------解决方案--------------------
你现在的表结构这样做不容易,应该建一个字段表示各自的目录层次
------解决方案--------------------
create table # (
id varchar(10),
name varchar(20))

insert #

select '01 ', '卷烟 '
union all
select '02 ', '白酒 '
union all
select '03 ', '瓜子 '
union all
select '01001 ', '云南卷烟厂 '
union all
select '01002 ', '昆明卷烟厂 '
union all
select '02001 ', '四川郎酒 '
union all
select '02002 ', '贵州茅台 '
union all
select '03001 ', '恰恰瓜子 '
union all
select '03002 ', '大好大瓜子 '
declare cur_ cursor for
select id,name from # order by id
declare @id_old varchar(10),@name_old varchar(20),@id_new varchar(10),@name_new varchar(20)
declare @p varchar(20)
select @p = '- '

open cur_

fetch next from cur_ into @id_old,@name_old
select @id_new = @id_old
select @name_new = @name_old
if len(@id_new) = 2
select @p = '- '
else if len(@id_new) > len(@id_old)
select @p = @p + '- '
else if len(@id_new) < len(@id_old)
select @p = left(@p,len(@p) - 1)
print @p + @name_new
while(@@fetch_status = 0)
begin

fetch next from cur_ into @id_new,@name_new

if len(@id_new) = 2
select @p = '- '
else if len(@id_new) > len(@id_old)
select @p = @p + '- '
else if len(@id_new) < len(@id_old)
select @p = left(@p,len(@p) - 1)
print @p + @name_new

select @id_old = @id_new
select @name_old = @name_new

end

close cur_
deallocate cur_

drop table #
------------------------------
结果

(所影响的行数为 9 行)

-卷烟
--云南卷烟厂
--昆明卷烟厂
-白酒
--四川郎酒
--贵州茅台
-瓜子
--恰恰瓜子
--大好大瓜子
--大好大瓜子
------解决方案--------------------

?
select * from table where len(CommClsssID)=2