Select t_4.*
From (Select t_2.*
 From Extend_Daily_Report t_2
 Where t_2.Dr_User_Id = '1920'
 Union All
 Select t_0.*
 From Extend_Daily_Report t_0, Extend_Daily_Task t_1
 Where t_0.Dr_Id = t_1.Dr_Id And t_1.Dt_Pm_Id = '1920' And (t_0.Dr_Status = '1' Or t_0.Dr_Status = '2') And
 t_0.Dr_Dept_Id <> '2008' 
 Union All
 Select t_3.*
 From Extend_Daily_Report t_3
 Where (t_3.Dr_Status = '1' Or t_3.Dr_Status = '2') And t_3.Dr_Dept_Id = '2008' And t_3.dr_user_id<>'1920') t_4工作中写了一个这样得 sql 实现了我的需求,感觉用union 不妥。由于小弟sql很差求教各位给出一个不用union all 的 sql 来实现这个功能。多谢

解决方案 »

  1.   

    select t.* from Extend_Daily_Report t, Extend_Daily_Task t_1
    where t.Dr_Id = t_1.Dr_Id 
    and dr_User_id='1920' 
    And (t.Dr_Dept_Id <> '2008' and (t.Dr_Status = '1' Or t.Dr_Status = '2') )
    and ((t.Dr_Status = '1' Or t.Dr_Status = '2') And t.Dr_Dept_Id = '2008' And t.dr_user_id<>'1920')
      

  2.   

    Select t_0.*
    From Extend_Daily_Report t_0, Extend_Daily_Task t_1
    Where 
    t_0.Dr_User_Id = '1920' or
    ((t_0.Dr_Status = '1' Or t_0.Dr_Status = '2') And t_0.Dr_Dept_Id = '2008' And  t_0.dr_user_id<>'1920') or
    (t_0.Dr_Id = t_1.Dr_Id And t_1.Dt_Pm_Id = '1920' And (t_0.Dr_Status = '1' Or t_0.Dr_Status = '2') And t_0.Dr_Dept_Id <> '2008' )
      

  3.   

    用union all 效率上没什么影响吧,不用的话如果是一张表就把所有条件集合到一起就行了!