2011-02-06 34 views
0

表:服務:TSQL:JOIN,肯定有可能 - 但不知道如何

ServiceID ServiceName 
1   Cleaning 
2   Painting 
3   Driving 
4   Cooking 

表:現役軍人:

ServiceMemberID MemberID ServiceID 
1    10  3 

查詢:

SELECT s.ServiceName, sm.MemberID 
FROM Services s 
JOIN ServiceMembers sm ON s.ServiceID = sm.ServiceID 
WHERE sm.MemberID = 10 

退貨:

Driving 10 

問題: 如何連接表,這樣我就可以得到以下的TSQL:

Cleaning  NULL 
Painting  NULL 
Driving  10 
Cooking  NULL 

在此先感謝。

回答

2

這應做到:

 
SELECT s.ServiceName, sm.MemberID 
FROM Services s 
    LEFT JOIN ServiceMembers sm ON s.ServiceID = sm.ServiceID AND sm.MemberID = 10 
+0

+1。你太快了:) – a1ex07 2011-02-06 23:50:55

相關問題