2013-02-25 295 views
0

我的查詢看起來像這樣。假設它運行得很好.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

回答

0

是的,你可以給在where子句只要你是內給予有效條件的任何數量的表之間的比較你的where子句。我想你應該是指SQL JOINS

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