MySQL

MySQL IntroductionComment MySQLInstall MySQL container with Docker-ComposeMySQL AdminMySQL ALTER TABLEMySQL ArithmeticMySQL BackticksMySQL Backup using mysqldumpMySQL Change PasswordMySQL Character Sets and CollationsMySQL clientMySQL ClusteringMySQL Configuration and tuningMySQL Connecting with UTF-8 Using Various Programming language.MySQL Converting from MyISAM to InnoDBMySQL Create New UserMySQL Creating databasesMySQL Customize PS1MySQL Data TypesMySQL Date and Time OperationsMySQL Dealing with sparse or missing dataMySQL DELETEMySQL Drop TableMySQL Dynamic Un-Pivot Table using Prepared StatementMySQL ENUMMySQL Error 1055: ONLY FULL GROUP BY: something is not in GROUP by clause ...MySQL Error codesMySQL EventsMySQL Extract values from JSON typeMySQL Full-Text searchMySQL Group byMySQL Handling Time ZonesMySQL Indexes and KeysMySQL INSERTMySQL JoinsMySQL JOINS: Join 3 table with the same name of id.MySQL JSONMySQL Limit and OffsetMySQL LOAD DATA INFILEMySQL LOCK TABLEMySQL Log filesMySQL Many-to-many Mapping tableMySQL MyISAM EngineMySQL mysqlimportMySQL NULLMySQL One to ManyMySQL ORDER byMySQL PartitioningMySQL Performance TipsMySQL Performance TuningMySQL Pivot queriesMySQL PREPARE StatementsMySQL Recover from lost root passwordMySQL Regular ExpressionsMySQL ReplicationMySQL Reserved WordsMySQL Security via GRANTsMySQL SELECTMySQL Server InformationMySQL SSL Connection SetupMySQL Stored routines (procedures and functions)MySQL String operationsMySQL Table CreationMySQL Temporary TablesMySQL Time with subsecond precisionMySQL TransactionMySQL TRIGGERSMySQL UNIONMySQL UnionsMySQL UPDATEMySQL Using VariablesMySQL VIEWRecover and reset the default root password for MySQL 5.7+



MySQL Unions

From WikiOD

Syntax[edit | edit source]

  • 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[edit | edit source]

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.

Union operator[edit | edit source]

The UNION operator is used to combine the result-set (only distinct values) of two or more SELECT statements.

Query: (To selects all the different cities (only distinct values) from the "Customers" and the "Suppliers" tables)

SELECT City FROM Customers
UNION
SELECT City FROM Suppliers
ORDER BY City;

Result:

Number of Records: 10

City
------
Aachen
Albuquerque
Anchorage
Annecy
Barcelona
Barquisimeto
Bend
Bergamo
Berlin
Bern

Union ALL[edit | edit source]

UNION ALL to select all (duplicate values also) cities from the "Customers" and "Suppliers" tables.

Query:

SELECT City FROM Customers
UNION ALL
SELECT City FROM Suppliers
ORDER BY City;

Result:

Number of Records: 12

City
-------
Aachen
Albuquerque
Anchorage
Ann Arbor
Annecy
Barcelona
Barquisimeto
Bend
Bergamo
Berlin
Berlin
Bern

UNION ALL With WHERE[edit | edit source]

UNION ALL to select all(duplicate values also) German cities from the "Customers" and "Suppliers" tables. Here Country="Germany" is to be specified in the where clause.

Query:

SELECT City, Country FROM Customers
WHERE Country='Germany'
UNION ALL
SELECT City, Country FROM Suppliers
WHERE Country='Germany'
ORDER BY City;

Result:

Number of Records: 14
City Country
Aachen Germany
Berlin Germany
Berlin Germany
Brandenburg Germany
Cunewalde Germany
Cuxhaven Germany
Frankfurt Germany
Frankfurt a.M. Germany
Köln Germany
Leipzig Germany
Mannheim Germany
München Germany
Münster Germany
Stuttgart Germany

Credit:Stack_Overflow_Documentation