MySQL query to include more than one column in a table that doesn't already exist

MySQLMySQLi Database

You can easily add more than one column that does not exist in a query using multiple AS keywords.

Let us first create a table. The query to create a table is as follows −

mysql> create table ColumnDoesNotExists
   -> (
   -> UserId int,
   -> UserName varchar(20)
   -> );
Query OK, 0 rows affected (0.67 sec)

Insert some records in the table using insert command. The query is as follows −

mysql> insert into ColumnDoesNotExists(UserId,UserName) values(100,'Larry');
Query OK, 1 row affected (0.14 sec)
mysql> insert into ColumnDoesNotExists(UserId,UserName) values(101,'Sam');
Query OK, 1 row affected (0.22 sec)
mysql> insert into ColumnDoesNotExists(UserId,UserName) values(102,'Mike');
Query OK, 1 row affected (0.15 sec)
mysql> insert into ColumnDoesNotExists(UserId,UserName) values(103,'David');
Query OK, 1 row affected (0.15 sec)
mysql> insert into ColumnDoesNotExists(UserId,UserName) values(104,'Robert');
Query OK, 1 row affected (0.10 sec)
mysql> insert into ColumnDoesNotExists(UserId,UserName) values(105,'Maxwell');
Query OK, 1 row affected (0.20 sec)
mysql> insert into ColumnDoesNotExists(UserId,UserName) values(106,'Bob');
Query OK, 1 row affected (0.17 sec)
mysql> insert into ColumnDoesNotExists(UserId,UserName) values(107,'John');
Query OK, 1 row affected (0.17 sec)
mysql> insert into ColumnDoesNotExists(UserId,UserName) values(108,'James');
Query OK, 1 row affected (0.18 sec)

Example

Display all records from the table using a select statement. The query is as follows −

mysql> select *from ColumnDoesNotExists;

Output

+--------+----------+
| UserId | UserName |
+--------+----------+
|    100 | Larry    |
|    101 | Sam      |
|    102 | Mike     |
|    103 | David    |
|    104 | Robert   |
|    105 | Maxwell  |
|    106 | Bob      |
|    107 | John     |
|    108 | James    |
+--------+----------+
9 rows in set (0.00 sec)

Example

Here is the query to add a column name that does not exist in a query −

mysql> select UserId,UserName,23 AS Age from ColumnDoesNotExists;

Output

+--------+----------+-----+
| UserId | UserName | Age |
+--------+----------+-----+
|    100 | Larry    |  23 |
|    101 | Sam      |  23 |
|    102 | Mike     |  23 |
|    103 | David    |  23 |
|    104 | Robert   |  23 |
|    105 | Maxwell  |  23 |
|    106 | Bob      |  23 |
|    107 | John     |  23 |
|    108 | James    |  23 |
+--------+----------+-----+
9 rows in set (0.00 sec)

Example

Now let us see the query to add more than one column at a time. Here we are adding columns ‘Marks’ and ‘CountryName’ −

mysql> select UserId,UserName,23 AS Age,99 AS Marks,'UK' AS CountryName from ColumnDoesNotExists;

Output

+--------+----------+-----+-------+-------------+
| UserId | UserName | Age | Marks | CountryName |
+--------+----------+-----+-------+-------------+
|    100 |    Larry |  23 |    99 |          UK |
|    101 |      Sam |  23 |    99 |          UK |
|    102 |     Mike |  23 |    99 |          UK |
|    103 |    David |  23 |    99 |          UK |
|    104 |   Robert |  23 |    99 |          UK |
|    105 |  Maxwell |  23 |    99 |          UK |
|    106 |      Bob |  23 |    99 |          UK |
|    107 |     John |  23 |    99 |          UK |
|    108 |    James |  23 |    99 |          UK |
+--------+----------+-----+-------+-------------+
9 rows in set (0.00 sec)
raja
Published on 19-Mar-2019 13:15:14
Advertisements