Is RIGHT JOIN Ever Required?

后端 未结 7 1097
你的背包
你的背包 2020-12-09 18:48

Do any queries exist that require RIGHT JOIN, or can they always be re-written with LEFT JOIN?

And more specifically, how do you re-write this one without the right

7条回答
  •  温柔的废话
    2020-12-09 19:22

    I use LEFT JOINs about 99.999% of the time, but some of my dynamic code generation uses RIGHT JOINs which mean that the stuff outside the join doesn't need to be reversed.

    I'd also like to add that the specific example you give I believe produces a cross join, and that is probably not your intention or even a good design.

    i.e. I think it's effectively the same as:

    SELECT *
    FROM t1
    CROSS JOIN t3
    LEFT JOIN t2
        ON t1.k2 = t2.k2
        AND t3.k3 = t2.k3
    

    And also, because it's a cross join, there's not a lot the optimizer is going to be able to do.

提交回复
热议问题