开发者

TSQL: JOIN, for sure it is possible -- but don't know how

开发者 https://www.devze.com 2023-02-09 11:54 出处:网络
TABLE: Services: ServiceID ServiceName 1Cleaning 2Painting 3Driving 4Cooking TABLE: ServiceMembers: ServiceMemberIDMem开发者_运维百科berID ServiceID

TABLE: Services:

ServiceID ServiceName  
1         Cleaning  
2         Painting  
3         Driving  
4         Cooking  

TABLE: ServiceMembers:

ServiceMemberID  Mem开发者_运维百科berID ServiceID  
1                10       3

Query:

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

Returns:

Driving    10

Question: How to JOIN tables, so I can get following in TSQL:

Cleaning      NULL
Painting      NULL
Driving       10
Cooking       NULL

Thanks in advance.


This should do it:

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

精彩评论

暂无评论...
验证码 换一张
取 消