What is the difference between UNION and UNION ALL?

后端 未结 26 2403
伪装坚强ぢ
伪装坚强ぢ 2020-11-21 11:28

What is the difference between UNION and UNION ALL?

26条回答
  •  再見小時候
    2020-11-21 12:15

    You can avoid duplicates and still run much faster than UNION DISTINCT (which is actually same as UNION) by running query like this:

    SELECT * FROM mytable WHERE a=X UNION ALL SELECT * FROM mytable WHERE b=Y AND a!=X

    Notice the AND a!=X part. This is much faster then UNION.

提交回复
热议问题