可以使用递归查询,类似查找bom

解决方案 »

  1.   

    http://www.cnblogs.com/wenjl520/archive/2010/01/18/1650393.html
      

  2.   

    http://www.cnblogs.com/xfrog/archive/2010/10/10/1847462.html参考下;还有一种写存储过程,使用游标,把数据存进临时表,在返回;但是麻烦了点。
      

  3.   


    create table TESTTABLE_A
    (
    ID varchar2(50),
    NAME varchar2(50),
    TOPLEVEL varchar2(50)
    )insert into TESTTABLE_A values('1','家具','0');
    insert into TESTTABLE_A values('2','建材','0');
    insert into TESTTABLE_A values('3','卧室','1');
    insert into TESTTABLE_A values('4','客厅','1');
    insert into TESTTABLE_A values('5','餐厅','1');
    insert into TESTTABLE_A values('6','床','3');
    insert into TESTTABLE_A values('7','床垫','3');
    insert into TESTTABLE_A values('8','床头柜','3');select  *
    From TESTTABLE_A
    Start With TOPLEVEL = '0' Connect By Prior ID=TOPLEVEL-----------------------------------------------------------
    ID                          NAME                          TOPLEVEL
    1                           家具                                0
    3                           卧室                                1
    6                           床                                    3
    7                           床垫                                3
    8                           床头柜                            3
    4                           客厅                                1
    5                           餐厅                                1
    2                           建材                                0
      

  4.   

    'TOPLEVEL' 附近的语法不正确。如果它要用作公用表表达式,需要使用分号显式终止前一个语句、、
    大哥这是为什么
      

  5.   

    你还是用程序做吧 就是他们说的递归 :查询出子集的sql 然后循环 判断是否有子集 如果有 就再次调用本身这个方法·不过你要记得add
      

  6.   

    可以用递归写个存储过程实现,甚至可以使用sqlserver的一些cube,rollup函数。
    但是,你这个表结构没有体现出层级关系的字段,比如3,我这么知道对应哪个父类目录?就只有2层?
      

  7.   

    使用自查询就可以了。
     SELECT  b.ID ,
                            b.CategoryName ,
                            b.OrganID
                    FROM    dbo.WebCategory a
                            JOIN dbo.WebCategory b ON b.OrganID LIKE a.OrganID
                                                      + '%'
    这是我自己用过的 你可以参考下 
      

  8.   

    楼主如果用SQL完成不了的话,可以试着在列里面加一个为父级编号,在程序里面使用递归算法或者使用下面的方法:http://www.2cto.com/database/201209/155630.html
      

  9.   

    with cte_table1 as
    (
    select * from table1
    union all
    select * from table1 as c inner join cte_table1 as p on c.TopLevel = p.ID
    )
    select * from cte_table1 where id=3;
    sql2005以及以上可以用,sql2000的话要写程序递归或迭代了。
      

  10.   


    使用 UNION、INTERSECT 或 EXCEPT 运算符合并的所有查询必须在其目标列表中有相同数目的表达式。
      

  11.   

    你这个应该是和qq空间多级留言一样的问题吧,看我发的帖子
    http://bbs.csdn.net/topics/390725979
      

  12.   

    select * from TEMPFCYTEST 
    start with parentid is null 
    connect by prior id = parentid有个函数,专门处理这内问题
      

  13.   


    使用 UNION、INTERSECT 或 EXCEPT 运算符合并的所有查询必须在其目标列表中有相同数目的表达式。select c.* from table1 as c inner join cte_table1 as p on c.TopLevel = p.ID
      

  14.   


    'TopLEvel' 附近的语法不正确。如果它要用作公用表表达式,需要使用分号显式终止前一个语句。
      

  15.   


    使用 UNION、INTERSECT 或 EXCEPT 运算符合并的所有查询必须在其目标列表中有相同数目的表达式。select c.* from table1 as c inner join cte_table1 as p on c.TopLevel = p.ID还是提示哪个,不对啊
      

  16.   


    使用 UNION、INTERSECT 或 EXCEPT 运算符合并的所有查询必须在其目标列表中有相同数目的表达式。select c.* from table1 as c inner join cte_table1 as p on c.TopLevel = p.ID还是提示哪个,不对啊把你写的语句 贴出来
      

  17.   


    使用 UNION、INTERSECT 或 EXCEPT 运算符合并的所有查询必须在其目标列表中有相同数目的表达式。select c.* from table1 as c inner join cte_table1 as p on c.TopLevel = p.ID还是提示哪个,不对啊把你写的语句 贴出来
    with cte_table1 as
    (
    select * from GoodsTags
    union all
    select * from GoodsTags as c inner join cte_table1 as p on c.TopLevel = p.ID
    )
    select * from cte_table1
      

  18.   


    使用 UNION、INTERSECT 或 EXCEPT 运算符合并的所有查询必须在其目标列表中有相同数目的表达式。select c.* from table1 as c inner join cte_table1 as p on c.TopLevel = p.ID还是提示哪个,不对啊把你写的语句 贴出来
    with cte_table1 as
    (
    select * from GoodsTags
    union all
    select * from GoodsTags as c inner join cte_table1 as p on c.TopLevel = p.ID
    )
    select * from cte_table1改为
    select  c.* from GoodsTags as c inner join cte_table1 as p on c.TopLevel = p.ID
      

  19.   


    使用 UNION、INTERSECT 或 EXCEPT 运算符合并的所有查询必须在其目标列表中有相同数目的表达式。select c.* from table1 as c inner join cte_table1 as p on c.TopLevel = p.ID还是提示哪个,不对啊把你写的语句 贴出来
    with cte_table1 as
    (
    select * from GoodsTags
    union all
    select * from GoodsTags as c inner join cte_table1 as p on c.TopLevel = p.ID
    )
    select * from cte_table1改为
    select  c.* from GoodsTags as c inner join cte_table1 as p on c.TopLevel = p.ID真的可以了,谢谢,但是这是为什么呢
      

  20.   


    使用 UNION、INTERSECT 或 EXCEPT 运算符合并的所有查询必须在其目标列表中有相同数目的表达式。select c.* from table1 as c inner join cte_table1 as p on c.TopLevel = p.ID还是提示哪个,不对啊把你写的语句 贴出来
    with cte_table1 as
    (
    select * from GoodsTags
    union all
    select * from GoodsTags as c inner join cte_table1 as p on c.TopLevel = p.ID
    )
    select * from cte_table1改为
    select  c.* from GoodsTags as c inner join cte_table1 as p on c.TopLevel = p.ID
    还是不行,只是能出结果了,但是结果的顺序不对,我要的是列一个toplevel是0然后下边跟着他的子类的
      

  21.   


    使用 UNION、INTERSECT 或 EXCEPT 运算符合并的所有查询必须在其目标列表中有相同数目的表达式。select c.* from table1 as c inner join cte_table1 as p on c.TopLevel = p.ID还是提示哪个,不对啊把你写的语句 贴出来
    with cte_table1 as
    (
    select * from GoodsTags
    union all
    select * from GoodsTags as c inner join cte_table1 as p on c.TopLevel = p.ID
    )
    select * from cte_table1改为
    select  c.* from GoodsTags as c inner join cte_table1 as p on c.TopLevel = p.ID
    还是不行,只是能出结果了,但是结果的顺序不对,我要的是列一个toplevel是0然后下边跟着他的子类的select * from GoodsTags where toplevel=0 
    union all  要自己想问题
      

  22.   

    我看上面都弄得挺复杂的,我觉得一句简单的条件查询就可以了吧
    select (case when TopLevel =0 then ID else TopLevel end) as 排序列,* from category 
    order by 排序列,ID 
      

  23.   


    使用 UNION、INTERSECT 或 EXCEPT 运算符合并的所有查询必须在其目标列表中有相同数目的表达式。select c.* from table1 as c inner join cte_table1 as p on c.TopLevel = p.ID还是提示哪个,不对啊把你写的语句 贴出来
    with cte_table1 as
    (
    select * from GoodsTags
    union all
    select * from GoodsTags as c inner join cte_table1 as p on c.TopLevel = p.ID
    )
    select * from cte_table1改为
    select  c.* from GoodsTags as c inner join cte_table1 as p on c.TopLevel = p.ID
    还是不行,只是能出结果了,但是结果的顺序不对,我要的是列一个toplevel是0然后下边跟着他的子类的select * from GoodsTags where toplevel=0 
    union all  要自己想问题我也很想自己想问题,但是无从想起啊,这都是第一次见的东西
      

  24.   


    使用 UNION、INTERSECT 或 EXCEPT 运算符合并的所有查询必须在其目标列表中有相同数目的表达式。select c.* from table1 as c inner join cte_table1 as p on c.TopLevel = p.ID还是提示哪个,不对啊把你写的语句 贴出来
    with cte_table1 as
    (
    select * from GoodsTags
    union all
    select * from GoodsTags as c inner join cte_table1 as p on c.TopLevel = p.ID
    )
    select * from cte_table1改为
    select  c.* from GoodsTags as c inner join cte_table1 as p on c.TopLevel = p.ID
    还是不行,只是能出结果了,但是结果的顺序不对,我要的是列一个toplevel是0然后下边跟着他的子类的select * from GoodsTags where toplevel=0 
    union all  要自己想问题你这个出的结果也不对,算了,我还是递归吧,不走捷径了。
      

  25.   

    写代码去处理吧  也是用递归
    大致可以这样写:
     StringBuilder str = new StringBuilder();
    public void GetTree()
    {
        先取得根节点的数据源,假设是个DataTable dt
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                           str.Append(GetChildTree(dr["ID"].ToString()));
                     }
                  }
        }public string GetChildTree(string ID)
    {
             StringBuilder str1 = new StringBuilder();
           根据ID取得子节点的数据源,假设是个DataTable dt
                 if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        str1.Append(GetChildTree(dr["ID"].ToString()));
                     }
                  }
             return str1.ToString();
    }
    纯手写的代码,凑合着看吧,LZ可以参考下
      

  26.   


    只适合两层的,三层的就不对了
    自连接一次就可以处理3层分类
    select  (case when TopLevel =0 then ID  when  pTopLevel=0 then TopLevel else pTopLevel end) as 排序列,(case when pTopLevel =0 then ID   else TopLevel end) as 排序列2,*  from 
    (
    select a.*,b.TopLevel as pTopLevel from category a left join  category b on a.TopLevel = b.ID
    ) temp
    order by 排序列,排序列2,ID 
      

  27.   


    只适合两层的,三层的就不对了
    自连接一次就可以处理3层分类
    select  (case when TopLevel =0 then ID  when  pTopLevel=0 then TopLevel else pTopLevel end) as 排序列,(case when pTopLevel =0 then ID   else TopLevel end) as 排序列2,*  from 
    (
    select a.*,b.TopLevel as pTopLevel from category a left join  category b on a.TopLevel = b.ID
    ) temp
    order by 排序列,排序列2,ID 
    32个赞