总表allt字段:aaID,bbID,ccID,ddID,eeID,ffID 表taa字段:aaID,aaNAME 表tbb字段:bbID,bbNAME …… 想从allt中 select 出对应的:aaNAME,bbNAME,ccNAME,ddNAME,eeNAME,ffNAME 要用一条语句返回这些值。 
最近在写个程序,遇到这个问题,对sql语法不是很熟练,一时半会没想明白,特来发帖

解决方案 »

  1.   

    selct aaid,(select aaNAME from taa a where a.aaID=aa.aaID) as aaNAME ,
    bbid,(select bbNAME from taa a where a.bbID=aa.bbID) as bbNAME ,
    ccid,(select ccNAME from taa a where a.ccID=aa.ccID) as ccNAME ,
    ddid,(select ddNAME from taa a where a.ddID=aa.ddID) as ddNAME ,
    eeid,(select eeNAME from taa a where a.eeID=aa.eeID) as eeNAME ,
    ffid,(select ffNAME from taa a where a.ffID=aa.ffID) as ffNAME from allt aa
      

  2.   

    1对1情况select (select aaNAME from taa where aaid=t.aaid) as aaNAME ,
           (select bbNAME from tbb where bbid=t.bbid) as bbNAME ,
    ...
           (select ffNAME from tff where ffid=t.ffid) as ffNAME 
    from allt t1对多的话可以加MAX,TOP 1 之类的函数
      

  3.   

    selct aaid,(select aaNAME from taa a where a.aaID=aa.aaID) as aaNAME ,
    bbid,(select bbNAME from tbb b where b.bbID=aa.bbID) as bbNAME ,
    ccid,(select ccNAME from tcc c where c.ccID=aa.ccID) as ccNAME ,
    ddid,(select ddNAME from tdd d where d.ddID=aa.ddID) as ddNAME ,
    eeid,(select eeNAME from tee e where e.eeID=aa.eeID) as eeNAME ,
    ffid,(select ffNAME from tff f where f.ffID=aa.ffID) as ffNAME from allt aa