SQL Inner Join On Null Values

前端 未结 7 1854
天命终不由人
天命终不由人 2020-12-08 04:16

I have a Join

SELECT * FROM Y
INNER JOIN X ON ISNULL(X.QID, 0) = ISNULL(y.QID, 0) 

Isnull in a Join like this makes it slow.

7条回答
  •  粉色の甜心
    2020-12-08 04:46

    You could also use the coalesce function. I tested this in PostgreSQL, but it should also work for MySQL or MS SQL server.

    INNER JOIN x ON coalesce(x.qid, -1) = coalesce(y.qid, -1)
    

    This will replace NULL with -1 before evaluating it. Hence there must be no -1 in qid.

提交回复
热议问题