Query中的Where子句

时间:2013-02-25 03:40:33

标签: mysql sql

我的查询看起来像这样。假设它运行得很好。我混淆了 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 

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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