MySQL UNION

30% OFF - 9th Anniversary discount on Entity Framework Extensions until December 15 with code: ZZZANNIVERSARY9

Syntax

  • UNION DISTINCT -- dedups after combining the SELECTs
  • UNION ALL -- non dedup (faster)
  • UNION -- the default is DISTINCT
  • SELECT ... UNION SELECT ... -- is OK, but ambiguous on ORDER BY
  • ( SELECT ... ) UNION ( SELECT ... ) ORDER BY ... -- resolves the ambiguity

Remarks

UNION does not use multiple CPUs.

UNION always* involves a temp table to collect the results. *As of 5.7.3 / MariaDB 10.1, some forms of UNION deliver the results without using a tmp table (hence, faster).



Got any MySQL Question?