查询中的Where子句

问题描述:

我的查询看起来像这样。假设它运行得很好.im混淆了的最后部分Where where clause.Can我可以从两个不同的表中写出来吗?..我怎么写它,因为我想显示那些来自该日期范围的Active的工作人员。查询中的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 
+0

是不是已经在做你想做的事了?它在两个不同的表上测试条件。 – Patashu 2013-02-25 03:42:54

+0

为什么你在子查询中加入并使用这些连接进行外部查询? – andho 2013-02-25 03:44:44

+0

如果我没有弄错,是的,你可以 – c0dem0nkey 2013-02-25 03:46:44

是的,你可以给在where子句只要你是内给予有效条件的任何数量的表之间的比较你的where子句。我想你应该是指SQL JOINS

,只要你想从您的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