Sunday, February 17, 2013

multiple table join with where clause example

Sections
Section Table
SectionMembers
Section Members Table
MemberStatus
Member Status Table

select s.section_id,  s.title,  s.description,  m.status
from Sections s left join SectionMembers sm  on s.section_id = sm.section_id
  and sm.memberid = 200 left join MemberStatus m  on sm.status_code = m.status_code
where s.section_ownerid = 100;
 results as shown below
ote: while your desired result shows that section_id=2 has a status of ActiveMember there is no way in your sample data to make this value link to section 2.
This query gives the result:
| SECTION_ID |  TITLE | DESCRIPTION |         STATUS |
------------------------------------------------------
|          1 | title1 |       desc1 |  PendingMember |
|          2 | title2 |       desc2 | MemberRejected |
|          3 | title3 |       desc3 | MemberRejected |
|          4 | title4 |       desc4 |   ActiveMember |
|          5 | title5 |       desc5 |         (null) |
|          6 | title6 |       desc6 |         (null) |

No comments:

Post a Comment