字段说明:
id            产品编号
prod_name     产品名称
prod_ismain   主产品(1为主产品,此时prod_id为空,如果它为0则为副产品,他隶属于prod_id=id这个产品)prod_id       所在主产品的ID号(如果没有值,则说明它没有主产品)
prod_time

解决方案 »

  1.   

    最要要显示的是主产品的信息:
    id,prod_name,此主产品下的副产品的个数,prod_time
      

  2.   

    select id,prod_name,prod_ismain,prod_time,(select count(*) from tlb_product where prod_id=AA.id) from tlb_product AA where prod_ismain=1
      

  3.   

    能不能用游标方式实现?我是想学一下cursor
      

  4.   

    要解决这个问题,完全不用cursor,因为CURSOR这玩艺实在耗资源,要学嘛,当然可以尝试,也就是语法没错,就行了,也没什么,很简单,不过我看着实在有点别扭,大材小用了嘛,也望高手勿笑,呵呵!
    create proc aa
    as 
    begin
    declare @prod_main int,@prod_name varchar(20),@prod_time datetime
    create table #t1(id  int null,prod_name varchar(20) null,prod_count int null,prod_time datetime null) 
    declare c1 cursor for select  id,prod_name, prod_time from tbl_product
    open c1 
    fetch next from c1 into @prod_main,@prod_name,@prod_time
    while @@fetch_status = 0 
    begin
      select @prod_maincount = count(*) from tlb_product where  prod_id=@prod_main
       insert into #t1(id,proc_name,prod_count,prod_time)
        values(@prod_main,@prod_name,@prod_maincount,@prod_timd)
       fetch next from c1 into @prod_main,@prod_name,@prod_timeend
    deallocate c1
    select * from #t1
    end哎,怎么看怎么别扭啊!
      

  5.   

    select *,b.count from tbl_product inner join (select prod_id ,count(*) as count from tbl_product where prod_ismain=0 group by prid_id
    ) as b on tbl_product.id=b.prod_id
      

  6.   

    3ks hbwhwanghua(mikel) & all