
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Adding a column that doesn't exist in a query?
Add a column that does not exist in a query, with the help of AS keyword. The syntax is as follows −
SELECT yourColumnName1,yourColumnName2,....N,yourValue AS yourColumnName,....N' FROM yourTableName;
To understand the above syntax, let us 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)
Example
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)
Display all records from the table using 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)
- Related Articles
- MySQL query to include more than one column in a table that doesn't already exist
- Adding new column after a specific column and defining a default in MySQL?
- How to check if a column exist in a MySQL table?
- Adding integers from a variable to a MySQL column?
- MySQL query that returns a specific string if column is null?
- Adding a new column to existing DataFrame in Pandas in Python
- Find the value in an R data frame column that exist n times.
- Adding a new column to an existing DataFrame in Python Pandas
- MongoDB query to determine if a specific value does not exist?
- Name some non-metal that exist in a gaseous state?
- Adding a new column with the current time as a default value in MySQL?
- Adding a column whose value is not null by default in MySQL?
- MySQL query to replace a column value
- Calculate average of numbers in a column MySQL query?
- MySQL query to remove Null Records in a column?

Advertisements