0

I need to know the rows in TABLE A that have join records in TABLE B based a column value in TABLE B, but I also need to return rows in which a row in TABLE A has no match in TABLE B.

It seems like I need a LEFT JOIN and a LEFT OUTER JOIN, so I'm not sure what to do there. I understand how to do each, but don't understand how to do them together.

The schema looks like:

TABLE_A
pk

TABLE_B
pk
a_fk
some_value

I need the joined rows where Table_A has no join record in Table_B OR Table_A has a join record row in Table_B (it can have many) in which some_value does not equal "thisValue"

Thanks.

mstrom
  • 1,655
  • 3
  • 26
  • 41

1 Answers1

1

A Left join is a left outer join. Outer joins preserve one of the tables which is what you are after so good guess.

SELECT *
FROM Table A
LEFT JOIN Table B
ON TableA.Column = TableB.Column
AND B.SomeValue <> 'ThisValue'

All of the rows with a match will have the B information populated all of those without will have nulls in the B data

MarkD
  • 1,043
  • 12
  • 18