
- 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
Set 'alias' for all the column names in a single MySQL query
To set alias for column names, the syntax is as follows −
select yourColumnName1 anyAliasName1,yourColumnName2 anyAliasName2 from yourTableName anyAliasName;
To understand the above syntax, let us create a table −
mysql> create table DemoTable2014 -> ( -> FirstName varchar(20), -> LastName varchar(20) -> ); Query OK, 0 rows affected (0.70 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable2014 values('John','Smith'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable2014 values('David','Miller'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable2014 values('John','Doe'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable2014 values('Chris','Brown'); Query OK, 1 row affected (0.16 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable2014;
This will produce the following output −
+-----------+----------+ | FirstName | LastName | +-----------+----------+ | John | Smith | | David | Miller | | John | Doe | | Chris | Brown | +-----------+----------+ 4 rows in set (0.00 sec)
Here is the query to set 'alias' for column names in a single MySQL query −
mysql> select FirstName StudentFirstName,LastName StudentLastName -> from DemoTable2014 tbl;
This will produce the following output −
+------------------+-----------------+ | StudentFirstName | StudentLastName | +------------------+-----------------+ | John | Smith | | David | Miller | | John | Doe | | Chris | Brown | +------------------+-----------------+ 4 rows in set (0.00 sec)
- Related Articles
- Make all column names lower case in MySQL with a single query
- How to set all values in a single column MySQL Query?
- MySQL query to set current date in the datetime field for all the column values
- Setting column values as column names in the MySQL query result?
- Get multiple count in a single MySQL query for specific column values
- How to increment all the rows of a particular column by 1 in a single MySQL query (ID column +1)?
- Set all the columns of a MySQL table to a particular value with a single query
- Set Blank spaces in column names with MySQL?
- Set different IDs for records with conditions using a single MySQL query
- MySQL query to combine two columns in a single column?
- MySQL query to get the character length for all the values in a column?
- MySQL query to get the length of all columns and display the result in a single new column?
- MySQL query to return all items in a single row
- MySQL concat() to create column names to be used in a query?
- Can we use MySQL keyword as alias name for a column?

Advertisements