例如:
A(member,product,other)
B(member,product,price)A表记录:会员甲,20ml装飘柔洗发水,其他信息
会员甲,40ml装飘柔洗发水,其他信息会员乙,20ml装飘柔洗发水,其他信息
会员乙,40ml装飘柔洗发水,其他信息B表记录:
      ,20ml装飘柔洗发水,20.00
      ,40ml装飘柔洗发水,40.00
 甲   ,20ml装飘柔洗发水,5.00那么希望查出来的最后结果是:会员甲,20ml装飘柔洗发水,其他信息, 5.00
会员甲,40ml装飘柔洗发水,其他信息,40.00
会员乙,20ml装飘柔洗发水,其他信息,20.00
会员乙,40ml装飘柔洗发水,其他信息,40.00
THANKS!

解决方案 »

  1.   

    select * from 
    (
    select A.member,
           A.product,
           (select price from B where A.member = B.member and A.product = B.product) as temp
      from A
    ) tab_1 where temp  is not  nullunion
    select member,product,(select price from B where product = tab_2.product and B.member is null) from 
    (
    select  A.member,
    A.product,
    (select price  from B where A.member = B.member and A.product = B.product) as temp
     from A
    ) tab_2 where temp is  null
      

  2.   

    一个SQL语句解决问题,union第一部分,取出了存在本批订单中特价的部份,第二总价是非特价部分.整个SQL 我试过。但是我并没有加其实信息这个字段,你可以直接填进去就好了。这个不费事!
      

  3.   

    miocn(lala)你好,我按照你上面所示的方法测试过的结果是这样的:member   product         temp
    会员甲,20ml装飘柔洗发水, 5.00
    会员甲,40ml装飘柔洗发水, NULL
    会员乙,20ml装飘柔洗发水, NULL
    会员乙,40ml装飘柔洗发水, NULL也就是说,价格部分出了点问题.
      

  4.   


    create table #aa (member varchar(10),product varchar(50),other varchar(20),price varchar(10))
    insert into #aa
    select a.* ,b.price from a inner join (select price,product from b where member = '' )b on a.product=b.productif exists(select a.member,a.product,a.other,b.price from a inner join b on a.member=b.member and a.product=b.product )
    begin
    declare @member varchar(10),@product varchar(50),@price varchar(10)
    declare cur cursor for (select a.member,a.product,b.price from a inner join b on a.member=b.member and a.product=b.product)
    open cur
    fetch next from cur into @member,@product,@price
    while @@fetch_status=0
    begin
    update #aa set price=@price where member =@member and product=@product
    fetch next from cur into @member,@product,@price
    end
    close cur
    deallocate cur
    endselect * from #aa
      

  5.   

    刚才那个用
    select a.*,b.price from a left join b on a.member=b.member and a.product=b.product
    也可以得到。
      

  6.   

    select t1.member,t1.product,t2.price 
    from a t1 inner join b t2 on t1.member=t2.member and t1.product=t2.product
    union
    select member,product,(select price from B where product = tab_2.product and B.member is null) from 
    (
    select  A.member,
    A.product,
    (select price  from B where A.member = B.member and A.product = B.product) as temp
     from A 
    ) tab_2 where temp is  null
      

  7.   

    select a.member,b.product,b.price from A a,B b where a.member = b.member and a.product = b.productunionselect a.member,b.product,b.price from A a,B b where a.product = b.product and b.member is null
      

  8.   

    select * from 
    (
    select A.member,
           A.product,
           (select price from B where A.member = B.member and A.product = B.product) as temp
      from A
    ) tab_1 where temp  is not  nullunion
    select member,product,(select price from B where product = tab_2.product and B.member is null) from 
    (
    select  A.member,
    A.product,
    (select price  from B where A.member = B.member and A.product = B.product) as temp
     from A
    ) tab_2 where temp is  null以上所有的SQL是一个语句,
      

  9.   

    一起执行,我又试过,没有出错啊.我按照你的结构,一模一样的表和数据.我的是成功的.表A
    member           product
    1          1         
    1          2         
    1          3         
    2          1         
    2          2         
    2          3      
    表b
    member           product         price
    1          1          10
    1          2          5
    2          1          13
    NULL          2          9
    NULL          3          12结果真
    member     product    temp                 
    ---------- ---------- -------------------- 
    1          1          10
    1          2          5
    1          3          12
    2          1          13
    2          2          9
    2          3          12
      

  10.   

    zhongkeruanjian(编程亮子) (的做法,数据不对应,会产生多出几个结果的错误
      

  11.   

    miocn(lala)你好,我重新录入数据后发现得出的结果是正确的,可能是之前录入有问题.
    谢谢你!!!
      

  12.   

    select a.mem ,a.product,a.other,b.price from a,b where a.mem =b.mem and a.product =b.product
    union
    select a.mem ,a.product,a.other,b.price from a,b where  a.product =b.product and len(isnull(b.mem,'')) =0 
    and a.product not in(select a.product from a,b where a.mem =b.mem and a.product =b.product) 
    union 
    select a.mem ,a.product,a.other,b.price from a,b where  a.product =b.product and b.mem is null
    and a.mem not in(select a.mem from a,b where a.mem =b.mem and a.product =b.product)
      

  13.   

    回头再看  meet it another day