MySQL MySQL Unions

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

Syntax

  • SELECT column_name(s) FROM table1 UNION SELECT column_name(s) FROM table2;
  • SELECT column_name(s) FROM table1 UNION ALL SELECT column_name(s) FROM table2;
  • SELECT column_name(s) FROM table1 WHERE col_name="XYZ" UNION ALL SELECT column_name(s) FROM table2 WHERE col_name="XYZ";

Remarks

UNION DISTINCT is the same as UNION; it is slower than UNION ALL because of a de-duplicating pass. A good practice is to always spell out DISTINCT or ALL, thereby signaling that you thought about which to do.



Got any MySQL Question?