--查询
select A.code
       ,isnull(B.name,'''') as 'name'
from tableA A
left join tableB B on A.code=B.code
where B.name=(select top 1 name 
                from tableB 
                  where code=B.code 
                     order by (case flag  
                                  when 'I' then 1 
                                  when 'R' then 2 
                                  else 3 
                               end
                              )
             )
      or B.name is null--删除测试环境
drop table tableA,tableB--结果
/*
code       name       
---------- ---------- 
A          李四
B          找六
C          謝軍
D          '
E          '(5 row(s) affected)
*/

解决方案 »

  1.   

    select a.code,isnull((select top 1 name from tableb where code=a.code order by (case flag when 'I' then 0 when 'R' then 1 else 2 end)),'') as name
    from tablea a left join tableb b on a.code=b.code
    group by a.code
    /*
    结果
    code name
    ------------------
    A 李四
    B 找六
    C 謝軍
    D
    E */
      

  2.   

    --如果为空的name需要输出个''
    select a.code,isnull((select top 1 name from tableb where code=a.code order by (case flag when 'I' then 0 when 'R' then 1 else 2 end)),'''''') as name
    from tablea a left join tableb b on a.code=b.code
    group by a.code
    /*
    结果
    code name
    ------------------
    A 李四
    B 找六
    C 謝軍
    D ''
    E ''
    */
      

  3.   

    select A.code
           ,isnull(B.name,'''') as 'name'
    from tableA A
    left join tableB B on A.code=B.code
    where B.code=(select top 1 code  from tableB  where code=B.code   order by (case flag  
      when 'I' then 1   when 'R' then 2  else 3  end) )
      

  4.   

    select A.code
           ,isnull(B.name,'''') as 'name'
    from tableA A
    left join tableB B on A.code=B.code
    where B.name=(select top 1 name 
                    from tableB 
                      where code=B.code 
                         order by charindex(flag,'IRO')  --还有其他的就自己加
                                  )
          or B.name is null
      

  5.   

    这个charindex用的真是巧妙,果然是高人.
    select A.code
           ,isnull(B.name,'''') as 'name'
    from tableA A
    left join tableB B on A.code=B.code
    where B.name=(select top 1 name 
                    from tableB 
                      where code=B.code 
                         order by charindex(flag,'IRO')  --还有其他的就自己加
                                  )
          or B.name is null
      

  6.   

    select A.code,isnull(B.name,'''') as 'name' from tableA A
     left join tableB B on A.code=B.code
       where B.name=(select top 1 name from tableB where code=B.code 
       order by (case flag when 'I' then 1 when 'R' then 2  else 3 end))
          or B.name is null
      

  7.   

    引用zjcxc(邹建) ,牛
    select A.code
           ,isnull(B.name,'''') as 'name'
    from tableA A
    left join tableB B on A.code=B.code
    where B.name=(select top 1 name 
                    from tableB 
                      where code=B.code 
                         order by charindex(flag,'IRO')  --还有其他的就自己加
                                  )
          or B.name is null
      

  8.   

    charindex 引用的真是恰到好处.
      

  9.   

    如果使用了charindex(flag,'IRO'),那么需要对不存在'IRO'的行做出处理,因为这时返回值为0,排在了'I'的前边
      

  10.   

    zjcxc(邹建) 大哥,您的代码还有点问题.您看.
    create table tableA(id int ,code varchar(10));
    create table tableB(id int ,code varchar(10),name varchar(10),flag char(1));
    insert into tableA values(1,'A');
    insert into tableA values(2,'B');
    insert into tableA values(3,'C');
    insert into tableA values(4,'D');
    insert into tableA values(5,'E');insert into tableB values(90,'A','力钱','O');
    insert into tableB values(91,'A','王丹','O');
    insert into tableB values(99,'A','張三','O');
    insert into tableB values(16,'A','李四','I');
    insert into tableB values(13,'A','李四','R');
    insert into tableB values(66,'B','找六','I');
    insert into tableB values(567,'C','謝軍','I');select A.code
           ,isnull(B.name,'''') as 'name'
    from tableA A
    left join tableB B on A.code=B.code
    where B.name=(select top 1 name 
                    from tableB 
                      where code=B.code 
                         order by charindex(flag,'IRO')  --还有其他的就自己加
                                  )
          or B.name is null结果:A          李四
    A          李四
    B          找六
    C          謝軍
    D          ''
    E          ''//也就是李四重复了.怎么去掉这个重复的?
      

  11.   

    通過
    select A.code ,isnull(B.name,'''') as 'name'  from tableA A  left join tableB B on A.code=B.code where B.name=(select top 1 name from tableB   where code=B.code  order by (case flag  when  'I' then 1  when 'R' then 2  else 3 end ))  or B.name is null
    通過:
    select A.code
           ,isnull(B.name,'''') as 'name'
    from tableA A
    left join tableB B on A.code=B.code
    where B.name=(select top 1 name 
                    from tableB 
                      where code=B.code 
                         order by charindex(flag,'IRO'))//返回字符串中指定表达式的起始位置。
    or B.name is null
      

  12.   

    DISTINCT 阿!怎么反应不过来了阿!^_^!!
    select DISTINCT T.* from (
    select A.code
           ,isnull(B.name,'''') as 'name'
    from tableA A
    left join tableB B on A.code=B.code
    where B.name=(select top 1 name 
                    from tableB 
                      where code=B.code 
                         order by charindex(flag,'IRO')  --还有其他的就自己加
                                  )
          or B.name is null
    ) T