SQL Inner Join On Null Values

前端 未结 7 1849
天命终不由人
天命终不由人 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 05:02

    Basically you want to join two tables together where their QID columns are both not null, correct? However, you aren't enforcing any other conditions, such as that the two QID values (which seems strange to me, but ok). Something as simple as the following (tested in MySQL) seems to do what you want:

    SELECT * FROM `Y` INNER JOIN `X` ON (`Y`.`QID` IS NOT NULL AND `X`.`QID` IS NOT NULL);
    

    This gives you every non-null row in Y joined to every non-null row in X.

    Update: Rico says he also wants the rows with NULL values, why not just:

    SELECT * FROM `Y` INNER JOIN `X`;
    
    0 讨论(0)
提交回复
热议问题