查询病名是"病名1"的id,然后查询这个id下的yname字段中不包含"药1"的所有药,------->这句话理解不难
select *
from tb2
where id not in(select id from tb2 where ptname='病名1')
然后查询"药1"与查询出来的这些药同时在一个方中的次数. -->这句话什么理解?

解决方案 »

  1.   

    我是这样做的: 
    1.先查询出"病名1"的id,且yname字段中不包含"药1"的所有药,然后插入到临时表#TempTable(yname,num) 
    2.然后用游标,循环临时表,到表2里查询临时表里的药与"药1"同时出现的次数,然后更新临时表的num字段.最后#TempTable里的数据是这样的: 
    yname,num 
    药2  2 
    药3  3 
    药4  2 看你的做法,觉得不需要用到游标:select yname,count(*) num
    from tb2
    where yname<>'药1'
    group by yname;
      

  2.   

    ------------------------------------
    -- Author:happyflystone 
    -- Version:V1.001  
    -- Date:2009-03-12 22:54:15
    -------------------------------------- Test Data: ta
    If object_id('ta') is not null 
        Drop table ta
    Go
    Create table ta(id nvarchar(2),yname nvarchar(2))
    Go
    Insert into ta
     select '方1','药1' union all
     select '方1','药2' union all
     select '方1','药3' union all
     select '方1','药4' union all
     select '方2','药1' union all
     select '方2','药2' union all
     select '方3','药1' union all
     select '方3','药3' union all
     select '方4','药1' union all
     select '方4','药4' union all
     select '方4','药3' union all
     select '方5','药2' union all
     select '方5','药4' union all
     select '方5','药3' 
    Go
    --Start
    Select yname,count(1)
    from ta a
    where yname != '药1'
    and exists(select 1 from ta where id = a.id and yname = '药1')
    group by yname--Result:
    /*yname             
    ----- ----------- 
    药2    2
    药3    3
    药4    2(所影响的行数为 3 行)
    */
    --End 
      

  3.   

    create table tb1(id varchar(10), ptname varchar(10))
    insert into tb1 values('方1' , '病名1') 
    insert into tb1 values('方2' , '病名2') 
    insert into tb1 values('方3' , '病名3') 
    create table tb2(id varchar(10) , yname varchar(10))
    insert into tb2 values('方1' , '药1') 
    insert into tb2 values('方1' , '药2') 
    insert into tb2 values('方1' , '药3') 
    insert into tb2 values('方1' , '药4') 
    insert into tb2 values('方2' , '药1') 
    insert into tb2 values('方2' , '药2') 
    insert into tb2 values('方3' , '药1') 
    insert into tb2 values('方3' , '药3') 
    insert into tb2 values('方4' , '药1') 
    insert into tb2 values('方4' , '药4') 
    insert into tb2 values('方4' , '药3')
    goselect yname , count(*) num from
    (
    select distinct * from tb2 where yname in
    (select tb2.yname from tb1 , tb2 where tb1.id = tb2.id and tb1.ptname = '病名1' and tb2.yname <> '药1')
    union all
    select distinct * from tb2 where yname = '药1'
    ) m where yname <> '药1'
    group by yname drop table tb1 , tb2 /*
    yname      num         
    ---------- ----------- 
    药2         2
    药3         3
    药4         2(所影响的行数为 3 行)
    */
      

  4.   

    happyflystone写得很好,我补充下楼主要求的与病的关系declare  @t1 table([id] nvarchar(2),ptname nvarchar(20))
    Insert into @t1
     select '方1','病名1'union all
     select '方2','病名1' union all
     select '方3','病名2' 
     
    declare  @t2 table([id] nvarchar(2),yname nvarchar(20))
    Insert into @t2
     select '方1','药1' union all
     select '方1','药2' union all
     select '方1','药3' union all
     select '方1','药4' union all
     select '方2','药1' union all
     select '方2','药2' union all
     select '方3','药1' union all
     select '方3','药3' union all
     select '方4','药1' union all
     select '方4','药4' union all
     select '方4','药3' --union all
     --select '方5','药2' union all
     --select '方5','药4' union all
     --select '方5','药1' union all
     --select '方5','药3' select yname,COUNT(*) as num
    from @t2 a
    where 
    exists(select 1 from @t2 where id = a.id and yname = '药1')
    and yname in (select distinct yname from @t2 b,@t1 c where b.id =c.id and c.ptname = '病名1' and yname <>'药1')
    group by yname/*
    yname      num         
    ---------- ----------- 
    药2         2
    药3         3
    药4         2(所影响的行数为 3 行)
    */