0

我的查询看起来像这样。假设它运行良好。我混淆了Where子句的最后一部分。我可以从两个不同的表中写出来吗?..我怎么写呢,因为我想显示那些在那个日期范围内活跃的员工。

select d.Division,a.FirstName,
 (select count(h.id) from Department h
  inner join institution i on d.institution_id = i_Id
  ----
  ----
where i.institution_id =d.Id and h. date between @startDate and @endDate) as test

from Division d, inmate a
where d.Active = 1 and a.Active = 1

编辑我已经编辑了我的查询,最终看起来像这样..

select d.DivisionName,a.FirstName, (select count(h.id) from InHistory h inner join Institution i on h.Institution_id = i.Id inner join InType it on h.InType_id = it.Id inner join mate a on h.mate_id = a.Id where i.InstitutionRegion_id = d.Id and it.InTypeName like '%Staff%' and h.AdmissionDate between '18/02/2013' and '18/02/2013') as Admission from Division d, mate a where d.Active= 1 and a.Active =1 
4

2 回答 2

0

您可以在 SQL 查询的 WHERE 中添加任意数量的子句。

看一个例子

SELECT *
FROM employee inner join department on
    employee.DepartmentID = department.DepartmentID;
WHERE 
    employee.active = TRUE AND
    department.group = 3

http://en.wikipedia.org/wiki/Join_(SQL)#Inner_join

于 2013-02-25T04:41:34.457 回答
0

是的,您可以在 where 子句中对任意数量的表进行比较,前提是您在 where 子句中给出了有效条件。我认为你应该参考SQL JOINS

于 2013-02-25T04:05:28.480 回答