如何获得此查询

时间:2014-04-01 11:27:28

标签: sql sql-server left-join

我正在使用两个表empAM_ActionItems

表的结构是

emp

  • EMPID
  • 用户ID
  • empemai

AM_ActionItems

  • AssignedBy
  • AssignedTo
  • assigndate
  • DUEDATE
  • closedate

除了AssignedBy = AssignedTo = userid

之外没有任何关系
select * 
from (select * 
      from AM_ActionItems as amp 
      where amp.DueDate > convert(date, dateadd(day, 0, getdate()))
        and amp.DueDate <= convert(date, dateadd(day, +1, getdate()))
        and amp.closeddate is null) as emp_det 
left outer join 
    emp on AssignedBy = userid 

子查询正在运行

结果是

 AssignedBy     AssignedTo          AssignedDate    DueDate        closeddate
Ponne.trichy    Syed Jameel Pasha   2014-03-27      2014-04-02       NULL
VenkatBiroje    Rekha               2014-03-28      2014-04-02       NULL

问题是

assignedby = assignedto = userid表格中的

emp如何从emp表中获取所有列?

2 个答案:

答案 0 :(得分:0)

我认为这个查询会给你答案。

select emp.* 
from tbl_emp emp left join AM_ActionItems  amp
emp.userid=amp.assignedto self join AM_ActionItems  amp1
amp.assignedto =amp1.assignedto  
where amp.DueDate > convert(date, dateadd(day, 0, getdate()))
        and amp.DueDate <= convert(date, dateadd(day, +1, getdate()))
        and amp.closeddate is null

答案 1 :(得分:0)

您想要选择内部查询结果的分配者或受让人的所有用户?然后从emp中选择并使用EXISTS查找是否可以找到匹配的记录。

select * 
from emp
where exists
(
  select * 
  from AM_ActionItems as amp 
  where emp.empid in (AssignedBy, AssignedTo)
  and amp.DueDate > convert(date, dateadd(day, 0, getdate()))
  and amp.DueDate <= convert(date, dateadd(day, +1, getdate()))
  and amp.closeddate is null
);