
- 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
How to generate field in MySQL SELECT?
Use keyword AS for this. Let us first create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Name varchar(20) ); Query OK, 0 rows affected (3.16 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(Name) values('John'); Query OK, 1 row affected (0.50 sec) mysql> insert into DemoTable(Name) values('Robert'); Query OK, 1 row affected (0.29 sec) mysql> insert into DemoTable(Name) values('David'); Query OK, 1 row affected (0.54 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+----+--------+ | Id | Name | +----+--------+ | 1 | John | | 2 | Robert | | 3 | David | +----+--------+ 3 rows in set (0.00 sec)
Following is the query to generate field in MySQL SELECT −
mysql> select Id,Name,'US' AS CountryName from DemoTable;
Output
+----+--------+-------------+ | Id | Name | CountryName | +----+--------+-------------+ | 1 | John | US | | 2 | Robert | US | | 3 | David | US | +----+--------+-------------+ 3 rows in set (0.00 sec)
- Related Articles
- Concat a field in MySQL SELECT?
- MySQL query to generate row index (rank) in SELECT statement?
- How to select data in MySQL where a field has a minimum value?
- How to map keys to values for an individual field in a MySQL select query?
- Limit length of longtext field in MySQL SELECT results?
- How to select a single field in MongoDB?
- How to select a field corresponding to the field in which MAX() exists?
- How to convert char field to datetime field in MySQL?
- Select different fields in MySQL even if a field is set to null?
- Select a field and if it's null, select another with MySQL?
- Can we select field name in MySQL that contains an asterisk?
- How to Auto Generate Database Diagram in MySQL?
- MySQL select any one field out of two with respect to the value of a third field?
- Set an alternative of WHERE clause for each SELECT field in MySQL
- How to select last row in MySQL?

Advertisements