我有两个表,一个是大类表,一个是大类的分类表
大类表btable字段有 bid,name   
数据               1    a
                   2    aa
                   3    ccc
                   4    dd
分类表stable字段有 sid,bid,userid  其中bid是大类表的id字段的外键
数据               1    2     1
                   2    2     2
                   3    4     2
                   4    3     3
                   5    2     2
                   6    1     2
                   7    1     2
                   8    2     2
====================================================================
我想根据分类表中获得userid=2这样的数据
             bid  name  次数
              2   aa    3
              1   a     2
              4   dd    1也就是说,通过连表获取某个用户引用大类表的信息及次数。求实现此功能的最优算法。谢谢!!!

解决方案 »

  1.   

    select bid,name,count(1) as '次数' from btable a join stable b on a.bid=b.bid where b.userid=2
      

  2.   

    create table btable(bid int,name varchar(20))
    insert into btable select 1,'a'
    insert into btable select 2,'aa'
    insert into btable select 3,'ccc'
    insert into btable select 4,'dd'create table stable(sid int,bid int,userid int)
    insert into stable select 1,2,1
    insert into stable select 2,2,2
    insert into stable select 3,4,2
    insert into stable select 4,3,2insert into stable select 5,2,2
    insert into stable select 6,1,2
    insert into stable select 7,1,2
    insert into stable select 8,2,2--
    --我想根据分类表中获得userid=2这样的数据
    --            bid  name  次数
    --              2  aa    3
    --              1  a    2
    --              4  dd    1 select a.bid,a.name,count(1) as '次数' 
    from btable a join stable b on a.bid=b.bid 
    where b.userid=2
    group by a.bid,a.namebid name 次数
    1 a 2
    2 aa 3
    3 ccc 1
    4 dd 1
      

  3.   

    declare @btable table (bid int,name nvarchar(5))
    insert @btable select 1,   'a'
    union all select  2,    'aa'
    union all select  3,    'ccc'
    union all select  4,    'dd' declare @stable table (sid int,bid int,userid int)
    insert @stable select 1 ,   2 ,   1
     union all select                 2 ,   2,    2
      union all select                3  ,  4  ,  2
      union all select                4  ,  3 ,   3
      union all select                5  ,  2 ,   2
      union all select                6  ,  1 ,   2
      union all select                7  ,  1  ,  2
       union all select               8  ,  2 ,   2 select max(a.bid) as bid,name,count(a.bid) as '次数' 
    from @btable a left join @stable b on a.bid=b.bid 
    where b.userid=2 group by (name)
    /*
    bid           name  次数
    ----------- ----- -----------
    1           a     2
    2           aa    3
    4           dd    1*/
      

  4.   

    declare @btable table (bid int,name nvarchar(5))
    insert @btable select 1,   'a'
    union all select  2,    'aa'
    union all select  3,    'ccc'
    union all select  4,    'dd' declare @stable table (sid int,bid int,userid int)
    insert @stable select 1 ,   2 ,   1
     union all select                 2 ,   2,    2
      union all select                3  ,  4  ,  2
      union all select                4  ,  3 ,   3
      union all select                5  ,  2 ,   2
      union all select                6  ,  1 ,   2
      union all select                7  ,  1  ,  2
       union all select               8  ,  2 ,   2 select a.bid,a.name,count(1) as '次数' 
    from @btable a join @stable b on a.bid=b.bid 
    where b.userid=2 group by a.bid,a.name
    order by 次数 descbid name 次数
    2 aa 3
    1 a 2
    4 dd 1用楼上的数据,我的数据好象弄错了
      

  5.   

    可以说,这是最好的写法听人说 left join 比 join 也许好一点点点,但没测试过
      

  6.   

    数据库会吃得消吗???????? 没问题的 :) 假如这语句sql也吃不消,sql早就被丢弃了