A表(物品)       B表(处理方法)    C表(关联表)
AID  AName      BID   BName     CID  AID  BID
1    猪肉          1     红烧       1    1    1
2    牛肉          2     清蒸       2    1    2
3    菠菜          3     清汤       3    2    1
4    苹果          4     生吃      4    2    2
                                 5    3    3查询:物品有已有的方法和没有的方法:以猪肉为例,查询得到以下表
AID  AName  BID  AName  是否
1     猪肉    1     红烧    有
1    猪肉    2     清蒸    有
1    猪肉    3     清汤    无
1    猪肉    4     生吃    无
怎么实现?
谢谢

解决方案 »

  1.   

    create table A(AID int, AName varchar(10))
    insert into A values(1, '猪肉')
    insert into A values(2, '牛肉')
    insert into A values(3, '菠菜')
    insert into A values(4, '苹果')
    create table B(BID int, BName varchar(10))
    insert into B values(1, '红烧')
    insert into B values(2, '清蒸')
    insert into B values(3, '清汤')
    insert into B values(4, '生吃')
    create table C(CID int, AID int, BID int)
    insert into C values(1, 1, 1 )
    insert into C values(2, 1, 2 )
    insert into C values(3, 2, 1 )
    insert into C values(4, 2, 2 )
    insert into C values(5, 3, 3 )
    godeclare @AName as varchar(10)
    set @Aname = '猪肉'select m.* , isnull((select col = '有' from c where aid = m.aid and bid = m.bid),'无') 是否 from
    (
      select a.* , b.* from a,b where a.aname = @Aname
    ) m
     
    drop table A,B,C/*
    AID         AName      BID         BName      是否   
    ----------- ---------- ----------- ---------- ---- 
    1           猪肉         1           红烧         有
    1           猪肉         2           清蒸         有
    1           猪肉         3           清汤         无
    1           猪肉         4           生吃         无(所影响的行数为 4 行)
    */
      

  2.   

    select a.aid,
     a.aname,
    b.bid,
    b.bname,
    case when c.aid = 1 then '有' else '无' end
    from b left join a on 1= 1
    left join c on c.bid = b.bid and a.aid = c.aid
      

  3.   

    create table A(AID int, AName varchar(10))
    insert into A values(1, '猪肉')
    insert into A values(2, '牛肉')
    insert into A values(3, '菠菜')
    insert into A values(4, '苹果')
    create table B(BID int, BName varchar(10))
    insert into B values(1, '红烧')
    insert into B values(2, '清蒸')
    insert into B values(3, '清汤')
    insert into B values(4, '生吃')
    create table C(CID int, AID int, BID int)
    insert into C values(1, 1, 1 )
    insert into C values(2, 1, 2 )
    insert into C values(3, 2, 1 )
    insert into C values(4, 2, 2 )
    insert into C values(5, 3, 3 )
    goselect a.aid,
     a.aname,
    b.bid,
    b.bname,
    case when c.aid = 1 then '有' else '无' end
    from b left join a on 1= 1
    left join c on c.bid = b.bid and a.aid = c.aid
    where a.aname = '猪肉'
    drop table a,b,c/*
    aid         aname      bid         bname           
    ----------- ---------- ----------- ---------- ---- 
    1           猪肉         1           红烧         有
    1           猪肉         2           清蒸         有
    1           猪肉         3           清汤         无
    1           猪肉         4           生吃         无(所影响的行数为 4 行)
    */
      

  4.   

    select d.aname,d.bname,case when cid is null  then '无' else '有' end from (select * from a ,b where aname='猪肉') d left join c on d.bid=c.bid
     and d.aid=c.aid/*
    猪肉 红烧 有
    猪肉 清蒸 有
    猪肉 清汤 无
    猪肉 生吃 无
    */
      

  5.   

    create table A(AID int, AName varchar(10))
    insert into A values(1, '猪肉')
    insert into A values(2, '牛肉')
    insert into A values(3, '菠菜')
    insert into A values(4, '苹果')
    create table B(BID int, BName varchar(10))
    insert into B values(1, '红烧')
    insert into B values(2, '清蒸')
    insert into B values(3, '清汤')
    insert into B values(4, '生吃')
    create table C(CID int, AID int, BID int)
    insert into C values(1, 1, 1 )
    insert into C values(2, 1, 2 )
    insert into C values(3, 2, 1 )
    insert into C values(4, 2, 2 )
    insert into C values(5, 3, 3 )
    godeclare @AName as varchar(10)
    set @Aname = '猪肉'select m.* , isnull((select col = '有' from c where aid = m.aid and bid = m.bid),'无') 是否 from
    (
      select a.* , b.* from a,b where a.aname = @Aname
    ) m
     
    drop table A,B,C
      

  6.   

    create table A(AID int, AName varchar(10))
    insert into A values(1, '猪肉')
    insert into A values(2, '牛肉')
    insert into A values(3, '菠菜')
    insert into A values(4, '苹果')
    create table B(BID int, BName varchar(10))
    insert into B values(1, '红烧')
    insert into B values(2, '清蒸')
    insert into B values(3, '清汤')
    insert into B values(4, '生吃')
    create table C(CID int, AID int, BID int)
    insert into C values(1, 1, 1 )
    insert into C values(2, 1, 2 )
    insert into C values(3, 2, 1 )
    insert into C values(4, 2, 2 )
    insert into C values(5, 3, 3 )
    goselect a.*,b.*,case when c.cid is null then '无' else '有' end 是否 from a
    cross join b
    left join c
    on c.bid=b.bid and c.aid=a.aid
    where aName='猪肉'
    godrop table a
    drop table b
    drop table c
    go
      

  7.   

    select t.*, case when c.aid = 1 then '有' else '无' end
     from
    (select a.aid,a.aname,b.bid,b.bname from a_t a,b_t b) t
    left join c_t c
    on t.aid=c.aid and t.bid=c.bid