mysql union

The MYSQL UNION operator is made use of to integrate the result-set of 2 or more SELECT statements. Each SELECT statement within MYSQL UNION must have the very same number of columns. The columns must additionally have comparable data types The columns in each SELECT declaration need to also remain in the very same order

What is Mysql Union? SQL UNION Operator

In this article, you can know about mysql union here are the details below;

MYSQL UNION Syntax

SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;

MYSQL UNION ALL Syntax

The MYSQL UNION driver picks only distinctive worths by default. To enable duplicate values, make use of MYSQL UNION ALL:

SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2;

Keep in mind: The column names in the result-set are typically equal to the column names in the very first SELECT statement.

Demo Database

In this tutorial, we will utilize the popular Northwind example data source.

Below is a choice from the “Customers” table:

CustomerID CustomerName ContactName Address City PostalCode Country
1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany
2 Ana Trujillo Emparedados y helados Ana Trujillo Avda. de la Constitución 2222 México D.F. 05021 Mexico
3 Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico

As well as a selection from the “Suppliers” table:

SupplierID SupplierName ContactName Address City PostalCode Country
1 Exotic Liquid Charlotte Cooper 49 Gilbert St. London EC1 4SD UK
2 New Orleans Cajun Delights Shelley Burke P.O. Box 78934 New Orleans 70117 USA
3 Grandma Kelly’s Homestead Regina Murphy 707 Oxford Rd. Ann Arbor 48104 USA

SQL MYSQL UNION Example.

The adhering to SQL statement returns the cities (just distinctive worths) from both the “Customers” and also the “Suppliers” table:

Example.

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

Note: If some consumers or vendors have the very same city, each city will only be detailed as soon as, since MYSQL picks just distinctive worths. Usage MYSQL UNION ALL to additionally choose to replicate worths!

SQL MYSQL ALL Example

The complying with SQL declaration returns the cities (duplicate worths additionally) from both the “Customers” and also the “Suppliers” table:

Example.

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

SQL MYSQL UNION With WHERE

The adhering to SQL statement returns the German cities (just unique worths) from both the “Customers” as well as the “Suppliers” table:

Example.

SELECT City, Country FROM Customers
WHERE Country=‘Germany’
UNION
SELECT City, Country FROM Suppliers
WHERE Country=‘Germany’
ORDER BY City;

SQL UNION ALL With WHERE

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

MYSQL UNION ALL

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

Another MYSQL UNION Example.

The adhering to SQL statement details all consumers and distributors:

Example.

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

MYSQL UNION

SELECT ‘Customer’ AS Type, ContactName, City, Country
FROM Customers
UNION
SELECT ‘Supplier’, ContactName, City, Country
FROM Suppliers;

Notice the “AS Type” over – it is a pen name. SQL Aliases are utilized to give a table or a column a short-term name. Pen names only exist throughout the inquiry. So, below we have created a temporary column named “Type” that lists whether they get in touch with a person is a “Customer” or a “Supplier.”

Check out over other articles like:

LEAVE A REPLY

Please enter your comment!
Please enter your name here