三张表 结构如下, 
主表A   
id        pjid         tasktypeid      stateid
101      5              1                   11
102      6              2                   22
表B  
pjid    pjname
5        酒店台帐
6        超市台帐表 C 
id                    keyvalue       name         note
tasktypeid         1                 设计      任务类型
tasktypeid         2                 编码      任务类型
stateid            11               未完成    任务状态
stateid            22               已完成    任务状态需要的查询结果 :
id      pjid       pjname        tasktypeid         name     stateid       name
101       5        酒店台帐         1               设计       1           未完成
102       6        超市台帐         2               编码       2           已完成

解决方案 »

  1.   


    select 
        t.id,t.pjid,t1.pjname,t.tasktypeid,t2.name tasktypename,t.stateid,t3.name statename
    from A t 
    left join B t1 on t.pjid =t1.pjid
    left join C t2 on t2.id='tasktypeid' and t.tasktypeid=t2.keyvalue
    left join C t3 on t3.id='stateid' and t.stateid=t3.keyvalue
    order by t.id,t.pjid,t.tasktypeid,t.stateid
      

  2.   


    select a.id, 
    a.pjid,
    (select pjname from 表B b where a.pjid=b.pjid) as pjname,
    a.tasktypeid, 
    (select name from 表C c where a.tasktypeid=c.keyvalue and c.id='tasktypeid') as tasktype_name,
    a.stateid
    (select name from 表C c where a.stateid=c.keyvalue and c.id='stateid') as state_name
    from 主表A  a
      

  3.   

    select A.id A.pjid,
           (select B.pjname from B where B.pjid = A.pjid) pjname,
           A.tasktypeid,
           (select C.name from C where c.id = 'tasktypeid'and C.keyvalue = A.tasktypeid) taskName,
           A.stateid,
           (select c.name from C where c.id = 'stateid 'and C.keyvalue = A.tasktypeid) stateName
      from A
      

  4.   

    对于经常写sql做报表的我来说太简单了吧,
    select a.*,b.*,c.* from a,b,(select * from c where note='任务类型') d,
    (select * from c where note='任务状态') e
    where a.pjid=b.pjid
    and a.tasktypeid=d.keyvalue
    and a.stateid=e.keyvalue
    需要什么字段自己选择吧