表结构
表(product)
产品编号   样式编号  产品名称       分类编号   是否有效(1为有效)
pid(pk)    psid      pname          pcid           isvalid
int        int       nvarchar(50)   int           bit表(style)
样式编号  样式名称     排序     父样式编号
psid(pk)  name         sortid   ppsid 
int       nvarchar(50) int      int
 
数据
表(product)
1     1    aa   1   1   
2     2    bb   1   1
3     3    cc   1   1
4     4    dd   1   1
5     5    ee   2   1
6     6    ff   1   1
7     7    gg   1   1
8     8    hh   1   1
9     1    ii   1   1
10    2    jj   1   1
11    3    kk   1   1
12    4    ll   1   1
13    5    mm   1   1
14    6    nn   1   1
15    7    oo   1   1
16    8    pp   1   1
17    1    qq   1   1
18    2    rr   1   1
19    3    ss   1   1
20    4    tt   1   1
21    5    uu   1   1
22    6    vv   1   1
23    7    ww   1   1
24    8    xx   1   1
25    1    yy   1   1
26    2    zz   1   1
27    3    ab   3   1
28    4    ac   1   1
29    5    ad   1   1
30    6    ae   1   1
31    7    af   1   0
32    8    ag   1   1表(style)
1    a1    0    0
2    b1    0    11
3    c1    0    12
4    d1    0    13
5    e1    0    12
6    f1    0    11
7    g1    0    0
8    h1    0    12
9    i1    0    11
10   j1    0    0
11   k1    0    0
12   l1    0    0
13   m1    0    0
14   n1    0    0现在我要查询产品有效,分类编号(参数@pcid int)例如为1的产品样式,sql如下:
select * from style where psid in (select distinct psid  from product where pcid=1 and isvalid=1)
数据如下
1    a1    0    0
2    b1    0    11
3    c1    0    12
4    d1    0    13
5    e1    0    12
6    f1    0    11
7    g1    0    0
8    h1    0    12如果我要查询产品有效,分类编号(参数@pcid int)例如为1的产品样式的父样式
如果样式为第一级直接获得,加上子样式的父样式
SQL该怎么写才能最优
得到的数据如下
1    a1    0    0
7    g1    0    0
11   k1    0    0
12   l1    0    0
13   m1    0    0

解决方案 »

  1.   

    --> 测试数据: [product]
    if object_id('[product]') is not null drop table [product]
    create table [product] (pid int,psid int,pname varchar(2),pcid int,isvalid int)
    insert into [product]
    select 1,1,'aa',1,1 union all
    select 2,2,'bb',1,1 union all
    select 3,3,'cc',1,1 union all
    select 4,4,'dd',1,1 union all
    select 5,5,'ee',2,1 union all
    select 6,6,'ff',1,1 union all
    select 7,7,'gg',1,1 union all
    select 8,8,'hh',1,1 union all
    select 9,1,'ii',1,1 union all
    select 10,2,'jj',1,1 union all
    select 11,3,'kk',1,1 union all
    select 12,4,'ll',1,1 union all
    select 13,5,'mm',1,1 union all
    select 14,6,'nn',1,1 union all
    select 15,7,'oo',1,1 union all
    select 16,8,'pp',1,1 union all
    select 17,1,'qq',1,1 union all
    select 18,2,'rr',1,1 union all
    select 19,3,'ss',1,1 union all
    select 20,4,'tt',1,1 union all
    select 21,5,'uu',1,1 union all
    select 22,6,'vv',1,1 union all
    select 23,7,'ww',1,1 union all
    select 24,8,'xx',1,1 union all
    select 25,1,'yy',1,1 union all
    select 26,2,'zz',1,1 union all
    select 27,3,'ab',3,1 union all
    select 28,4,'ac',1,1 union all
    select 29,5,'ad',1,1 union all
    select 30,6,'ae',1,1 union all
    select 31,7,'af',1,0 union all
    select 32,8,'ag',1,1
    --> 测试数据: [style]
    if object_id('[style]') is not null drop table [style]
    create table [style] (psid int,name varchar(2),sortid int,ppsid int)
    insert into [style]
    select 1,'a1',0,0 union all
    select 2,'b1',0,11 union all
    select 3,'c1',0,12 union all
    select 4,'d1',0,13 union all
    select 5,'e1',0,12 union all
    select 6,'f1',0,11 union all
    select 7,'g1',0,0 union all
    select 8,'h1',0,12 union all
    select 9,'i1',0,11 union all
    select 10,'j1',0,0 union all
    select 11,'k1',0,0 union all
    select 12,'l1',0,0 union all
    select 13,'m1',0,0 union all
    select 14,'n1',0,0
    gowith wsp 
    as
    (
    select * from style where psid in (select psid  from product where pcid=1 and isvalid=1)
    union all
    select a.* from style a,wsp b where a.psid=b.ppsid
    )
    select distinct * from wsp where ppsid=0 order by psid/*结果:
    psid        name sortid      ppsid
    ----------- ---- ----------- -----------
    1           a1   0           0
    7           g1   0           0
    11          k1   0           0
    12          l1   0           0
    13          m1   0           0
    */
      

  2.   

    ---2005递归
    USE tempdb
    GO-- 建立演示环境
    CREATE TABLE Dept(
     id int PRIMARY KEY, 
     parent_id int,
     name nvarchar(20))
    INSERT Dept
    SELECT 0, 0, N'<全部>' UNION ALL
    SELECT 1, 0, N'财务部' UNION ALL
    SELECT 2, 0, N'行政部' UNION ALL
    SELECT 3, 0, N'业务部' UNION ALL
    SELECT 4, 0, N'业务部' UNION ALL
    SELECT 5, 4, N'销售部' UNION ALL
    SELECT 6, 4, N'MIS' UNION ALL
    SELECT 7, 6, N'UI' UNION ALL
    SELECT 8, 6, N'软件开发' UNION ALL
    SELECT 9, 8, N'内部开发'
    GO-- 查询指定部门下面的所有部门
    DECLARE @Dept_name nvarchar(20)
    SET @Dept_name = N'MIS'
    ;WITH
    DEPTS AS(
     -- 定位点成员
     SELECT * FROM Dept
     WHERE name = @Dept_name
     UNION ALL
     -- 递归成员, 通过引用CTE自身与Dept基表JOIN实现递归
     SELECT A.*
     FROM Dept A, DEPTS B
     WHERE A.parent_id = B.id
    )
    SELECT * FROM DEPTS
    GO-- 删除演示环境
    DROP TABLE Dept本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/fredrickhu/archive/2009/09/19/4569529.aspx
      

  3.   

    create table tb(id varchar(3) , pid varchar(3) , name varchar(10)) 
    insert into tb values('001' , null  , '广东省') 
    insert into tb values('002' , '001' , '广州市') 
    insert into tb values('003' , '001' , '深圳市') 
    insert into tb values('004' , '002' , '天河区') 
    insert into tb values('005' , '003' , '罗湖区') 
    insert into tb values('006' , '003' , '福田区') 
    insert into tb values('007' , '003' , '宝安区') 
    insert into tb values('008' , '007' , '西乡镇') 
    insert into tb values('009' , '007' , '龙华镇') 
    insert into tb values('010' , '007' , '松岗镇') 
    go --查询指定节点及其所有子节点的函数 
    create function f_cid(@ID varchar(3)) returns @t_level table(id varchar(3) , level int) 
    as 
    begin 
      declare @level int 
      set @level = 1 
      insert into @t_level select @id , @level 
      while @@ROWCOUNT > 0 
      begin 
        set @level = @level + 1 
        insert into @t_level select a.id , @level 
        from tb a , @t_Level b 
        where a.pid = b.id and b.level = @level - 1 
      end 
      return 
    end 
    go --调用函数查询001(广东省)及其所有子节点 
    select a.* from tb a , f_cid('001') b where a.id = b.id order by a.id 
    /* 
    id  pid  name      
    ---- ---- ---------- 
    001  NULL 广东省 
    002  001  广州市 
    003  001  深圳市 
    004  002  天河区 
    005  003  罗湖区 
    006  003  福田区 
    007  003  宝安区 
    008  007  西乡镇 
    009  007  龙华镇 
    010  007  松岗镇 (所影响的行数为 10 行) 
    */ --调用函数查询002(广州市)及其所有子节点 
    select a.* from tb a , f_cid('002') b where a.id = b.id order by a.id 
    /* 
    id  pid  name      
    ---- ---- ---------- 
    002  001  广州市 
    004  002  天河区 (所影响的行数为 2 行) 
    */ --调用函数查询003(深圳市)及其所有子节点 
    select a.* from tb a , f_cid('003') b where a.id = b.id order by a.id 
    /* 
    id  pid  name      
    ---- ---- ---------- 
    003  001  深圳市 
    005  003  罗湖区 
    006  003  福田区 
    007  003  宝安区 
    008  007  西乡镇 
    009  007  龙华镇 
    010  007  松岗镇 (所影响的行数为 7 行) 
    */ drop table tb 
    drop function f_cid
      

  4.   

    有没人介绍下
    with的用法,作用
    没用过with
    有点看不懂
    或给上面语句加上注释
      

  5.   

    2005的语法
    2000的话
    -->Title:Generating test data
    -->Author:wufeng4552
    -->Date :2009-09-30 08:52:38
    set nocount on
    if object_id('tb','U')is not null drop table tb
    go
    create table tb(ID int, ParentID int)
    insert into tb select 1,0  
    insert into tb select 2,1  
    insert into tb select 3,1  
    insert into tb select 4,2  
    insert into tb select 5,3  
    insert into tb select 6,5  
    insert into tb select 7,6
    -->Title:查找指定節點下的子結點
    if object_id('Uf_GetChildID')is not null drop function Uf_GetChildID
    go
    create function Uf_GetChildID(@ParentID int)
    returns @t table(ID int)
    as
    begin
       insert @t select ID from tb where ParentID=@ParentID
       while @@rowcount<>0
       begin
          insert @t select a.ID from tb a inner join @t b
          on a.ParentID=b.id and 
          not exists(select 1 from @t where id=a.id)
       end 
    return
    end
    go
    select * from dbo.Uf_GetChildID(5)
    /*
    ID
    -----------
    6
    7
    */
    -->Title:查找指定節點的所有父結點
    if object_id('Uf_GetParentID')is not null drop function Uf_GetParentID
    go
    create function Uf_GetParentID(@ID int)
    returns @t table(ParentID int)
    as
    begin
       insert @t select ParentID from tb where ID=@ID
       while @@rowcount!=0
       begin
         insert @t select a.ParentID from tb a inner join @t b
           on a.id=b.ParentID and 
           not exists(select 1 from @t where ParentID=a.ParentID)
       end
      return
    end
    go
    select * from dbo.Uf_GetParentID(2)
    /*
    ParentID
    -----------
    1
    0
    */本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wufeng4552/archive/2009/09/30/4619995.aspx
      

  6.   

    逆推要实现查询一堆子级的父级注意:一堆子级可能有些就是父级
    比如
    1 aa 0
    2 bb 0
    3 cc 1
    4 dd 1
    5 ee 0
    而子级是
    2 bb 0
    3 cc 1
    4 dd 1
    5 ee 0
    要得到的结果是:
    1 aa 0
    2 bb 0
    5 ee 0