Mysql Merge

Mysql Merge Statement is used to merge the two sql statement using UNION clause.The UNION clause is used to combine the result set of any two sql queries.

Mysql Merge

Mysql Merge

     

Mysql Merge Statement is used to merge the two sql statement using UNION clause.The UNION clause is used to combine the result set of any two sql queries.

Understand with Example

The Tutorial covers you an example in 'Mysql Merge Statement'. To grasp this example we create a table 'employee01' and 'employee02' with required fieldnames and datatype respectively. To note, The Engine Value should be same in both the tables to merge them. The UNION clause will not work if the Engine value is different in both the tables.

Query to create table name employee01:

mysql> CREATE TABLE employee01 (
    ->              Empid int(10),
    ->              name varchar(30),
    ->              date date
    ->
    ->            )ENGINE=MyISAM;
Query OK, 0 rows affected (0.03 sec)

Query for inserting data in table name employee01:

The Query insert into is used to add the records or rows to the table' employee01'

mysql> insert into employee01(Empid,name,date)values('001','Girish','2009-01-06');
Query OK, 1 row affected (0.00 sec)

mysql> insert into employee01(Empid,name,date)values('002','Komal','2009-01-05');
Query OK, 1 row affected (0.02 sec)

mysql> insert into employee01(Empid,name,date)values('002','Mahendra','2009-01-04');
Query OK, 1 row affected (0.00 sec)

Query for viewing data of table named employee01:-

mysql> select * from employee01;
+-------+----------+------------+
| Empid | name     | date       |
+-------+----------+------------+
|     1 | Girish   | 2009-01-06 |
|     2 | Komal    | 2009-01-05 |
|     2 | Mahendra | 2009-01-04 |
+-------+----------+------------+
3 rows in set (0.00 sec)

Query to create table name employee02 :

mysql> CREATE TABLE employee02 (
    ->              Empid int(10),
    ->              name varchar(30),
    ->              date date
    ->            )ENGINE=MyISAM;
Query OK, 0 rows affected (0.03 sec)

Query for inserting data in table name employee02:

The insert into is used to add the records or rows to the table 'employee02'.

mysql> insert into employee02(Empid,name,date)values('001','A','2009-01-06');
Query OK, 1 row affected (0.00 sec)

mysql> insert into employee02(Empid,name,date)values('002','B','2009-01-05');
Query OK, 1 row affected (0.00 sec)

mysql> insert into employee02(Empid,name,date)values('003','C','2009-01-04');
Query OK, 1 row affected (0.01 sec)

mysql> insert into employee02(Empid,name,date)values('004','D','2009-01-03');
Query OK, 1 row affected (0.01 sec)

mysql> insert into employee02(Empid,name,date)values('005','E','2009-01-02');
Query OK, 1 row affected (0.00 sec)

mysql> insert into employee02(Empid,name,date)values('006','F','2009-01-01');
Query OK, 1 row affected (0.00 sec)

Query for viewing data of table named employee02:

mysql> select * from employee02;
+-------+------+------------+
| Empid | name | date       |
+-------+------+------------+
|     1 | A    | 2009-01-06 |
|     2 | B    | 2009-01-05 |
|     3 | C    | 2009-01-04 |
|     4 | D    | 2009-01-03 |
|     5 | E    | 2009-01-02 |
|     6 | F    | 2009-01-01 |
+-------+------+------------+
6 rows in set (0.00 sec)

Query to create table named Emptotal which is the merging of both the table name employee01 and employee02 :

The Query create a table 'Emptotal' that is the outcome merging of two table 'employee01' and 'employee02'.The UNION operator combine the result of two independent queries.

mysql> CREATE TABLE Emptotal (
    ->              Empid int(10),
    ->              name varchar(30),
    ->              date date
    ->            ) ENGINE=MERGE UNION=(employee01,employee02) ;
Query OK, 0 rows affected (0.03 sec)

Query for viewing data of merged table named Emptotal:

mysql> select * from Emptotal;
+-------+----------+------------+
| Empid | name     | date       |
+-------+----------+------------+
|     1 | Girish   | 2009-01-06 |
|     2 | Komal    | 2009-01-05 |
|     2 | Mahendra | 2009-01-04 |
|     1 | A        | 2009-01-06 |
|     2 | B        | 2009-01-05 |
|     3 | C        | 2009-01-04 |
|     4 | D        | 2009-01-03 |
|     5 | E        | 2009-01-02 |
|     6 | F        | 2009-01-01 |
+-------+----------+------------+
9 rows in set (0.00 sec)