Union clause in mysql

By | January 18, 2014

union means joining more than tables.It will not create problems if tables having different columns

Example:

Suppose we have three tables as below
Select * from mydoubts_personal;

————————————
|firstname | lastname | address |
————————————
|vasanthan | pv | india |
|nazar | gt | texas |
————————————-

Select * from mydoubts_buisness

————————————-
|last_name | first_name | address |
————————————-
|raju | raina | sreelanka|
|gt | nazar | texas |
|Martin | jacob | Dubai |
————————————-

Select * from mydoubts_enterprise

————————-
|institution | location |
————————-
|mydoubts.in | newyork |
|wordsgallery| Frankfurt| |
————————

Union Query as below

Select firstname,lastname,address from mydoubts_personal

UNION

Select first_name,last_name,address from mydoubts_buisness

UNION

Select institution,”,location from mydoubts_enterprise;

————————————
|firstname | lastname | address |
————————————
|vasanthan | pv | india |
|nazar | gt | texas |
|raju | raina | sreelanka|
|Martin | jacob | Dubai |
|mydoubts.in | | newyork |
|wordsgallery| | Frankfurt|
————————————-

Select firstname,lastname,address from mydoubts_personal

UNION ALL

Select first_name,last_name,address from mydoubts_buisness

UNION

Select institution,”,location from mydoubts_enterprise;
————————————
|firstname | lastname | address |
————————————
|vasanthan | pv | india |
|nazar | gt | texas |
|raju | raina | sreelanka|
|gt | nazar | texas |
|Martin | jacob | Dubai |
|mydoubts.in | | newyork |
|wordsgallery| | Frankfurt|
————————————-