SQL Inner Join On Null Values

前端 未结 7 1848
天命终不由人
天命终不由人 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.

    0 讨论(0)
  • 2020-12-08 04:53

    This article has a good discussion on this issue. You can use

    SELECT * 
    FROM Y
    INNER JOIN X ON EXISTS(SELECT X.QID 
                           INTERSECT 
                           SELECT y.QID);
    
    0 讨论(0)
  • 2020-12-08 04:57

    You have two options

    INNER JOIN x
       ON x.qid = y.qid OR (x.qid IS NULL AND y.qid IS NULL)
    

    or easier

    INNER JOIN x
      ON x.qid IS NOT DISTINCT FROM y.qid
    
    0 讨论(0)
  • 2020-12-08 04:59

    If you want null values to be included from Y.QID then Fastest way is

    SELECT * FROM Y LEFT JOIN X ON y.QID = X.QID

    Note: this solution is applicable only if you need null values from Left table i.e. Y (in above case).

    Otherwise INNER JOIN x ON x.qid IS NOT DISTINCT FROM y.qid is right way to do

    0 讨论(0)
  • 2020-12-08 05:02

    Are you committed to using the Inner join syntax?

    If not you could use this alternative syntax:

    SELECT * 
    FROM Y,X
    WHERE (X.QID=Y.QID) or (X.QUID is null and Y.QUID is null)
    
    0 讨论(0)
  • 2020-12-08 05:02

    I'm pretty sure that the join doesn't even do what you want. If there are 100 records in table a with a null qid and 100 records in table b with a null qid, then the join as written should make a cross join and give 10,000 results for those records. If you look at the following code and run the examples, I think that the last one is probably more the result set you intended:

    create table #test1 (id int identity, qid int)
    create table #test2 (id int identity, qid int)
    
    Insert #test1 (qid)
    select null
    union all
    select null
    union all
    select 1
    union all
    select 2
    union all
    select null
    
    Insert #test2 (qid)
    select null
    union all
    select null
    union all
    select 1
    union all
    select 3
    union all
    select null
    
    
    select * from #test2 t2
    join #test1 t1 on t2.qid = t1.qid
    
    select * from #test2 t2
    join #test1 t1 on isnull(t2.qid, 0) = isnull(t1.qid, 0)
    
    
    select * from #test2 t2
    join #test1 t1 on 
     t1.qid = t2.qid OR ( t1.qid IS NULL AND t2.qid IS NULL )
    
    
    select t2.id, t2.qid, t1.id, t1.qid from #test2 t2
    join #test1 t1 on t2.qid = t1.qid
    union all
    select null, null,id, qid from #test1 where qid is null
    union all
    select id, qid, null, null from #test2  where qid is null
    
    0 讨论(0)
提交回复
热议问题