SQL join two tables with specific condition

Viewed 9986

Table A structure:

enter image description here

Table B structure:

enter image description here

Above are two tables, TableB.TableARelationID is a relationID which used to map table A.

Desired output:

enter image description here

The desired result would be taking TableA.RecordID and TableB.Text, but only of Type 2 in table B, i.e. ignore Type 1

Below is the SQL query which I used:

SELECT tablea.recordid, 
       tableb.text 
FROM   tablea 
       LEFT JOIN tableb 
              ON tablea.relationid = tableb.tablearelationid 
WHERE  type = 2 

But the above query would output:

enter image description here

i.e. RecordID 1 was missing, as the "where" clause filtered.

So how can I show RecordID 1 from Table A?

7 Answers
Related