select distinct(a.f_id),a.* ,case f_opr_type when 0 then c.f_title when 1 then d.f_title when 2 then e.f_title end as title from M_USRLOG a,M_RAWLUT c, M_PROD d,M_DISPATCH e where (a.f_cnt_id = c.f_id and a.f_opr_type=0) or (a.f_cnt_id = d.f_id and a.f_opr_type=1) or (a.f_cnt_id = e.f_id and a.f_opr_type=2) or (a.f_opr_type=3)
  (由f_opr_type 对应不同表的f_title) 是不是复杂了,有没有简化的,这比子查询效率高吧? 另外一个问题是, 当c,d,e表中没有记录时不能正确工作,有什么解决办法?数据库不熟,半桶水.谢谢!

解决方案 »

  1.   

    http://blog.csdn.net/jinjazz/archive/2004/09/30/121456.aspx里面有多条例子可以参考
      

  2.   

    用left join连接别的表试试
      

  3.   

    用join 感觉好像不能满足吧,而且也更复杂了?
      

  4.   

    把这个问题放到数据库SQLSERVER中去问,邹建会帮你解决的
      

  5.   

    分成多个查询,再用union组合起来,这样会容易理解。。
      

  6.   

    select a.f_id,a.* ,c.f_title as title 
    from M_USRLOG a,M_RAWLUT c 
    where a.f_cnt_id = c.f_id and a.f_opr_type=0 
    union
    select a.f_id,a.* ,d.f_title as title 
    from M_USRLOG a,M_PROD d,M_DISPATCH e 
    where a.f_cnt_id = d.f_id and a.f_opr_type=1
    union
    select a.f_id,a.* ,e.f_title as title 
    from M_USRLOG a,M_DISPATCH e 
    where a.f_cnt_id = e.f_id and a.f_opr_type=2不知道搂住的a.f_opr_type=3 是干什么用的
      

  7.   

    谢谢各位,(a.f_opr_type=3时是什么表都不连),用union是清楚一些,但这两个效率比较怎么样呢?
      

  8.   

    http://www.39av.com/TG.asp?TGuser=yyq_leaf
    休息以下,看看电影吧,呵呵,免费的午餐!!