Ivan
Ivan

Reputation: 2579

Left join tables, add to on expression

I have a problem with left join two tables. Please help me. I have two tables: So if TableA is

A B
1 a
2 b
3 c

and TableB is

A B
1 d
2 e

Then Select * from TableA left join TableB on TableA.A = TableB.A returns

1 a 1 d
2 b 2 e
3 c null null

But: Select * from TableA left join TableB on TableA.A = TableB.A where TableB.B = 'e' returns:

2 b 2 e

And question, How I can get some like this:

2 b 2 e
3 c null null

Thank you very much.

Upvotes: 0

Views: 33

Answers (2)

Rachid B.
Rachid B.

Reputation: 388

Here you go

Select * from TableA left join TableB on TableA.A = TableB.A 
where TableB.B = 'e' OR TableB.B is NULL

And you will get null rows from tableB in addition of rows that match with Table.B='e'

Upvotes: 0

Giorgos Betsos
Giorgos Betsos

Reputation: 72175

It seems you want to get all matches from TableB, where B field is either equal to 'e' or NULL:

Select * 
from TableA 
left join TableB on TableA.A = TableB.A 
where (TableB.B = 'e') or (TableB.B is null)

Upvotes: 1

Related Questions