日期:2014-05-17  浏览次数:20501 次

根据ID查询有的,保留空的
一个表有2个ID,另外一个表存名字,如果有ID,就查询出Nmae,如果为NUll,就保留NULL
ID1     ID2
0623 NULL    
0470 1137      
NULL NULL  
0125    NULL     
想要的结果:可以将两列ID,省略,只要有Name2列就可以
ID1     ID2       Name1   Name2
0623 NULL      张三     NULL
0470 1137      李四     王五
NULL NULL      NULL     NULL
0125    NULL      NULL     赵六

只有 20分了,写大半天 没写出来, 在线结贴
------解决方案--------------------
  select (select name from table2 where ID=a.id1) name1,(select name from table2 where ID=a.id2) name2
from table1 as a
--table2 是名称表