2016-07-01 43 views
1

我想每個內加入像下面如何適用,如果在每個內部條件在SQL Server中加入

select * 
from table1 table 
inner join table2 t on t.column= table.column where condition 
inner join table3 tb on tb.column = table.column where condition 

但錯誤是在第二WHERE條件

語法不正確拋出申請條件靠近關鍵字'內部'。

在SQL Server中加入很新穎。請幫忙嗎?

+0

畢竟聯接哪裏都不病情後放每一次加入 – mohan111

回答

2

你需要把WHERE條款在聲明的結尾,但你可以在內部做這個連接是這樣的:

select * from table1 table 
inner join table2 t on t.column= table.column 
     and t.someColumn = 'SomeValue' --Here you can join on a condition 
inner join table3 tb on tb.column = t.column 
where <condition> 


--Or... 

select * from table1 table 
inner join table2 t on t.column= table.column 
inner join table3 tb on tb.column = t.column 
where 
t.column = 'blah' 
and tb.column = 'blah2' 
2

隨着inner join所有條件適用於完整的記錄集。所以,你可以使用left join時,只是把它們都在where條款

select * 
from table1 table 
inner join table2 t on t.column= table.column 
inner join table3 tb on tb.column = table.column 
where condition1 and condition2 

但例如條件僅適用於加入本身。所以,你可以使用on條款這樣

select * 
from table1 table 
left join table2 t on t.column = table.column AND condition1 
left join table3 tb on tb.column = table.column AND condition2 
0

如果您正在尋找讓您的主查詢乾淨,你可以使用一個CTE在邏輯上篩選表,從主客場查詢:

;with Employees as 
(
    select * from People where PersonType = 'Employee' 
) 
select * from ParkingSpots ps 
join Employees e on ps.PersonID = e.PersonID