
- 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
Using the value of an alias inside the same MySQL SELECT statement
You cannot directly use an alias in the SELECT. Instead, use a user-defined variable. Following is the syntax. Here, @yourAliasName is our variable and alias −
select @yourAliasName :=curdate() as anyAliasName,concat(‘yourValue.',yourColumnName,' yourValue',@yourAliasName) as anyAliasName from yourTableName;
Let us first create a table −
mysql> create table DemoTable ( Name varchar(40) ); Query OK, 0 rows affected (0.62 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('John Smith'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('Chris Brown'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('David Miller'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('John Doe'); Query OK, 1 row affected (0.18 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+--------------+ | Name | +--------------+ | John Smith | | Chris Brown | | David Miller | | John Doe | +--------------+ 4 rows in set (0.00 sec)
Following is the query to use the value of an alias inside the same SQL statement −
mysql> select @todayDate :=curdate() as todayDate,concat('Mr.',Name,' The current Date is=',@todayDate) as Result from DemoTable;
This will produce the following output −
+------------+------------------------------------------------+ | todayDate | Result | +------------+------------------------------------------------+ | 2019-09-08 | Mr.John Smith The current Date is=2019-09-08 | | 2019-09-08 | Mr.Chris Brown The current Date is=2019-09-08 | | 2019-09-08 | Mr.David Miller The current Date is=2019-09-08 | | 2019-09-08 | Mr.John Doe The current Date is=2019-09-08 | +------------+------------------------------------------------+ 4 rows in set (0.00 sec)
- Related Articles
- MySQL case statement inside a select statement?
- MySQL alias for SELECT * columns?
- Explain the use of SELECT DISTINCT statement in MySQL using Python?
- Add a percentage (%) sign at the end to each value while using MySQL SELECT statement
- Change value from 1 to Y in MySQL Select Statement using CASE?
- Select Statement to retrieve the same first names with similar last name (but different case) using MySQL?
- How to use alias in MySQL select query?
- How to select return value from MySQL prepared statement?
- Get table names using SELECT statement in MySQL?
- MySQL Select Rows where two columns do not have the same value?
- Change value from 1 to Y in MySQL Select Statement?
- Storing value from a MySQL SELECT statement to a variable?
- Select and Deselect Text Inside an Element using JavaScript
- What are the difference ways to replace nulls values in MySQL using SELECT statement?
- Perform filtering on an alias in MySQL?

Advertisements