How to replace a complex SQL MINUS query with LEFT OUTER JOIN equivalent

后端 未结 3 2003
轮回少年
轮回少年 2021-01-18 11:53

Trying to figure how how to replace the following, with equivalent left outer join:

select distinct(a.some_value)
from table_a a, table_b b
where a.id = b.a_         


        
3条回答
  •  野的像风
    2021-01-18 12:18

    Here's what my brain puts out after a beer:

    select distinct
        a.some_value
    from
        table_a a
        join table_b b on a.id = b.a_id
    where
        b.some_id = 123
        and b.create_date < '2014-01-01' 
        and b.create_date >= '2013-12-01'  
        and not exists (
            select
                a2.some_value
            from
                table_a a2
                join table_b b2 on a2.id = b2.a_id
            where
                b2.some_id = 123
                and b2.create_date < '2013-12-01'
        )
    

    Whether this'll optimize to faster than a left join or not is something I can't think of right now...

提交回复
热议问题