Union in MySQL
MySQL Union operation is used to combine two or more result sets. Before you use UNION operator in MySQL or MariaDB, you need to remember couple of things.
UNION result set will be Distinct records. You don't have to use Distinct. If result sets have duplicate records, UNION will return only distinct records.
- When you combine two or more result sets, all the result sets should have the same number of columns.
- The data type of the columns of each result set should be compatible or same like other results set columns.
Syntax :
Union in MySQL syntax
Select Column1,Column2,Column2 from TableOne
UNION
Select Column1,Column2,Column3 from TableTwo
UNION
Select Column1,Column2,Column2 from TableThree
Example :
Let's create couple of tables by using below scripts and then we use Union operator.
CREATE TABLE `customer` (
`idcustomer` int,
`firstname` varchar(50) NULL,
`lastname` varchar(30) NULL,
`age` int(11) DEFAULT NULL,
`phonenumber` char(11) DEFAULT NULL,
`dob` date DEFAULT NULL,
`gender` char(1) NOT NULL
) ;
insert into customer(idcustomer,firstname,lastname,age,phonenumber,dob,gender)
values
(1,'Raza','Ali',39,'505-4141969','1980-01-01','M'),
(2,'Aamir','Naz',39,'505-4141969','1980-01-01','M'),
(3,'Aamir','Shahzad',39,'505-4141900','1980-01-01','M'),
(4,'Aamir1','Shahzad',39,'505-4141900','1980-01-01','M'),
(5,'Robert','Ladson',69,'505-345900','1960-01-01','M');
create table customer4.
CREATE TABLE `customer4` (
`idcustomer` int,
`firstname` char(50) NULL,
`lastname` Nchar(30) NULL,
`age` int(3) DEFAULT NULL,
`phonenumber` char(11) DEFAULT NULL,
`dob` date DEFAULT NULL,
`gender` char(1) NOT NULL
) ;
insert into customer(idcustomer,firstname,lastname,age,phonenumber,dob,gender)
values
(1,'Raza','Ali',39,'505-4141969','1980-01-01','M'),
(5,'Aamir','Naz',39,'505-4141969','1980-01-01','M')
Let's union combine the rows or customer and customer4 table.
select * from customer
UNION
Select * from customer4;
CREATE TABLE `customer4` (
`idcustomer` int,
`firstname` char(50) NULL,
`lastname` Nchar(30) NULL,
`age` int(3) DEFAULT NULL,
`phonenumber` char(11) DEFAULT NULL,
`dob` date DEFAULT NULL,
`gender` char(1) NOT NULL
) ;
insert into customer(idcustomer,firstname,lastname,age,phonenumber,dob,gender)
values
(1,'Raza','Ali',39,'505-4141969','1980-01-01','M'),
(5,'Aamir','Naz',39,'505-4141969','1980-01-01','M')
Let's union combine the rows or customer and customer4 table.
select * from customer
UNION
Select * from customer4;
MySQL Union
Notice that in customer and customer4 some of the columns have different data types but it worked in UNION as the data is compatible. Also Union returned distinct records in result set from two tables.
Do you need Personal Finance?
ReplyDeleteBusiness Cash Finance?
Unsecured Finance
Fast and Simple Finance?
Quick Application Process?
Finance. Services Rendered include,
*Debt Consolidation Finance
*Business Finance Services
*Personal Finance services Help
contact us today and get the best lending service
personal cash business cash just email us below
Contact Us: financialserviceoffer876@gmail.com
call or add us on what's app +918929509036
Đặt vé máy bay tại Aivivu, tham khảo
ReplyDeletevé máy bay đi Mỹ
vé máy bay từ mỹ về việt nam bao nhiêu
vé máy bay từ nhật về việt nam bao nhiêu
bay từ đức về việt nam mấy tiếng
thông tin chuyến bay từ canada về việt nam
đặt vé máy bay từ hàn quốc về việt nam
khách sạn cách ly đà nẵng
chi phí vé máy bay cho chuyên gia nước ngoài
If you have a musculoskeletal injury or movement dysfunction, your healthcare provider may refer you to physical therapy to help decrease pain and improve mobility. Your symptoms may be mild, or you may have symptoms that severely limit your functional mobility.Find out more about how these physical therapy treatments are used in the field physical therapy athens al.
ReplyDeleteOnline gambling clubs, otherwise called virtual club or web gambling club are an internet based rendition of customary gambling clubs.출장안마추천
ReplyDelete출장샵매니저
Thank you for the thoughtful and educational post. It offers a plethora of information on the subject at hand. Also visit SASSA Status Check
ReplyDeleteThank you for the insightful and informative post. It provides a wealth of knowledge on the topic.
ReplyDeletehttps://rantiethnicity.com/jann-mardenborough-net-worth-a-rising-star-in-motorsports/
if you want to check celebrity net worth so clcik here
ReplyDeleteRead wealth about Rodney Carrington
ReplyDeleteCheck Net Worth
ReplyDeleteCheck your Favorit stars Net worth
ReplyDeleteUnlocking Potential: Strategies for Personal Growth and Empowerment Journey
ReplyDelete