Let us first create a table −
mysql> create table DemoTable1 -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(100) -> ); Query OK, 0 rows affected (0.76 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1(Name) values('Chris'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable1(Name) values('Robert'); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable1;
Output
+----+--------+ | Id | Name | +----+--------+ | 1 | Chris | | 2 | Robert | +----+--------+ 2 rows in set (0.00 sec)
Following is the query to create second table −
mysql> create table DemoTable2 -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(100) -> ); Query OK, 0 rows affected (0.48 sec)
Insert some records in the second table using insert command −
mysql> insert into DemoTable2(Name) values('David'); Query OK, 1 row affected (0.53 sec) mysql> insert into DemoTable2(Name) values('Bob'); Query OK, 1 row affected (0.15 sec)
Display all records from the second table using select statement −
mysql> select *from DemoTable2;
Output
+----+-------+ | Id | Name | +----+-------+ | 1 | David | | 2 | Bob | +----+-------+ 2 rows in set (0.00 sec)
Following is the query to add a new column to the union of two tables −
mysql> select Id,Name, 27 AS Age from DemoTable1 -> union -> select Id,name,20 AS Age from DemoTable2;
Output
+----+--------+-----+ | Id | Name | Age | +----+--------+-----+ | 1 | Chris | 27 | | 2 | Robert | 27 | | 1 | David | 20 | | 2 | Bob | 20 | +----+--------+-----+ 4 rows in set (0.00 sec)