Techolac - Computer Technology News
  • Home
  • Internet
  • Business
  • Computers
  • Gadgets
  • Lifestyle
  • Phones
  • Travel
  • Tech
  • More
    • Automotive
    • Education
    • Entertainment
    • Health
    • SEO
    • Linux
    • WordPress
    • Home Improvement
    • How to
    • Games
No Result
View All Result
  • Home
  • Internet
  • Business
  • Computers
  • Gadgets
  • Lifestyle
  • Phones
  • Travel
  • Tech
  • More
    • Automotive
    • Education
    • Entertainment
    • Health
    • SEO
    • Linux
    • WordPress
    • Home Improvement
    • How to
    • Games
No Result
View All Result
Techolac - Computer Technology News
No Result
View All Result
Home Tech

What is Mysql Union? SQL UNION Operator

by Luke
October 3, 2020
in Tech
Reading Time: 5 mins read
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!

See also  How Technology Transformed the World of Education

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:

  • 100 % Fixed | Solutions to DISM Error 87 Windows 10 / 8/7
  • How to Solve | Error code 50382 WM1 in Monster Hunter World
  • (According to 150 Experts) How to Learn SEO in 2020
  • {Solved} Secure Connection Failed PR_END_OF_FILE_ERROR on Firefox
  • OpenGL Error 1281 Invalid Value | How to Fix it

Related Posts

ecommerce website developers malaysia
Tech

Top 13 Ecommerce Website Developers Malaysia For 2022

June 25, 2022
Challenges faced with Mobile Test Automation
Tech

Challenges faced with Mobile Test Automation

May 19, 2022
6 Tech Moves Everyone Should Make In 2022
Tech

6 Tech Moves Everyone Should Make In 2022

April 16, 2022

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

game development company

Top 10 Game Development Company In 2022

June 30, 2022
What is Guest Blogging? Why it’s Important for Your Business

What is Guest Blogging? Why it’s Important for Your Business

June 30, 2022
Is Delta 8 Flowers Something You Are Interested In?

Is Delta 8 Flowers Something You Are Interested In?

June 29, 2022
referral marketing software

10 Best Referral Marketing Software & Tools In 2022

June 29, 2022
Content Marketing Strategies to Improve Your Sales

Content Marketing Strategies to Improve Your Sales

June 29, 2022
  • DashTech
  • TechDaddy
  • Terms and Conditions
  • Disclaimer
  • Write for us

© Techolac © Copyright 2019 - 2022, All Rights Reserved.

No Result
View All Result
  • Home
  • Internet
  • Business
  • Computers
  • Gadgets
  • Lifestyle
  • Phones
  • Travel
  • Tech
  • More
    • Automotive
    • Education
    • Entertainment
    • Health
    • SEO
    • Linux
    • WordPress
    • Home Improvement
    • How to
    • Games

© Techolac © Copyright 2019 - 2022, All Rights Reserved.

Go to mobile version