开发者

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:

  1. If Col3 is not null, then true iif Col1==Col3; otherwise
  2. 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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜