table1:people
id     name     deptid
1       a        12
2       b        13
3       c        table2:department
id         name
12         x1
13         x2
14         x3我用这样的sql语句(select a.id,a.name,b.deptname from people a,department b where a.deptid = b.id),但是只能查出
id    name    deptname
1      a       x1
2      b       x2
如何使查询出的下面结果,即当people表中deptid为空时,能查询出下面结果,要在一句sql语句中实现的:
id    name    deptname
1      a       x1
2      b       x2
3      c       

解决方案 »

  1.   

    select a.id,a.name,b.deptname from people a
    left join department b on a.deptid = b.id
      

  2.   

    select a.id,a.name,b.deptname from people a left join department b on a.deptid = b.id
      

  3.   

    select a.id,a.name,nvl(b.deptname,'') from people a,department b where a.deptid = b.idnvl(b.deptname,'')这个作用是如果b.deptname不是null则返回b.deptname否则显示空
      

  4.   

    select a.id,a.name,b.deptname from people a,department b where a.deptid = b.id(+) ;