表a 
id   name 
1     ee
2     ff
3     dd
表b
id   pwd  des 
1    123   ddd
1    123 
2    123    
3    456   
现在我要得到如下结构
1. 假如现在条件是 pwd=123
结果如下:
id  name  pwd   des 
1    ee    123   des
1    ee    123
2    ff    123   
3    dd              
只要实现a left join b 的时候对b表有个条件的问题,要保证a表的数据都出来 b表只出来条件的部分

解决方案 »

  1.   

    select * from a left join b on a.id=b.id
    where b.pwd='123'
      

  2.   

    select a.id,a.name,c.pwd,c.des from a left join (select * from b where pwd='123') c on a.id=c.id
      

  3.   


    select * from (select * from a left join b on a.id=b.id) t where t.pwd='123' or t.pwd is null
      

  4.   

    waterfirer(水清)的应该没有问题吧?楼主试过了?
      

  5.   

    试过了  在sql 里没有问题
    但在sap 里不行了
    郁闷
      

  6.   

    select a.*,b.pwd,b.des from a left join b where b.pwd=123
      

  7.   

    create table a(
       id int,
       name char(10)
    )insert a 
      select '1','ee'
    union
      select '2','ff'
    union
      select '3','dd'create table b(
       id   int,
       pwd  int,
       des char(10)
    )select * from b
    insert b 
      select '1','123','ddd'
    union
      select '1','123',''
    union
      select '2','123',''
    union
      select '3','456',''select a.id,a.name,b.pwd,b.des from a
      left join b
             on a.id=b.id and b.pwd='123' 
      order by a.id,b.des descid          name       pwd         des        
    ----------- ---------- ----------- ---------- 
    1           ee         123         ddd       
    1           ee         123                   
    2           ff         123                   
    3           dd         NULL        NULL(所影响的行数为 4 行)
    是不是这样的结果!
    楼猪可以说的更清楚点吗!