How to write this T-SQL WHERE condition?
I've got two tables:
Tabl开发者_开发知识库eA
Col1
Col2
TableB
Col3
Col4
I want to join them together:
SELECT * from TableA join TableB ON (...)
Now, in place of ...
I need to write an expression that evaluates to:
- If Col3 is not null, then true iif Col1==Col3; otherwise
- If Col3 is null, then true iif Col2==Col4
What would be the most elegant way to do this?
ON (Col1=Col3 OR (Col3 IS NULL AND Col2=Col4))
should do the trick (if Col3 is null, Col1=Col3 cannot evalutate to TRUE)
Try this:
SELECT *
FROM TableA
JOIN TableB
ON Col1 = Col3
OR (Col3 IS NULL AND Col2 = Col4)
精彩评论