老师教我优化这个SQL语句  Select tb1.id,tb1.name,isnull((select name from tb5 where id=tb1.id),’’)
from tb1,tb2,tb3 
where tb1.id=tb2.pid and tb1.id=tb3.pid and tb1.id not in (select id from tb4)
group by tb1.id,tb1.name我不会写 希望各位大虾帮帮忙

解决方案 »

  1.   

    Select tb1.id,tb1.name,tb5.name 
    from tb1,tb5
    where  tb1.id=tb5.id and tb1.id not in (select id from tb4) 
    group by tb1.id不知道是不是你要的?
      

  2.   

    你这个是三表联合查询。
    Select tb1.id,tb1.name,isnull((select name from tb5 where id=tb1.id),’’) 
    from tb1 inner join tb1.id=tb2.pid 
    inner join  tb1.id=tb3.pid on tb1.id not in (select id from tb4) 
    group by tb1.id,tb1.name
    试试 
      

  3.   

    select a.id,a.name,isnull((select name from tb5 where id=a.id),'')
    from tb1 a inner join tb2 b on a.id=b.pid
    inner join tb3 c on a.id=c.pid
    where a.id not in (select id from tb4)
    group by a.id,a.name
      

  4.   

    select a.id, a.name, isnull(e.name,'') 
    from tb1 a inner join tb2 b on a.id=b.pid 
    inner join tb3 c on a.id=c.pid 
    inner join tb4 d on a.id<>d.id
    left join tb5 e on a.id=e.id
    group by a.id, a.name
      

  5.   

    Select tb1.id,tb1.name,isnull(tb5.name,’’) 
    from tb1 left join tb2 on tb1.id=tb2.pid 
    left join tb3  on tb1.id=tb3.pid 
    left join tb5  on on tb1.id=tb5.pid 
    where tb1.id not in (select id from tb4)
    group by tb1.id,tb1.name