How to generate field in MySQL SELECT?

MySQLMySQLi Database

<p>Use keyword AS for this. Let us first create a table &minus;</p><pre class="result notranslate">mysql&gt; create table DemoTable &nbsp; &nbsp;( &nbsp; &nbsp;Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, &nbsp; &nbsp;Name varchar(20) &nbsp; &nbsp;); Query OK, 0 rows affected (3.16 sec)</pre><p>Insert some records in the table using insert command &minus;</p><pre class="prettyprint notranslate">mysql&gt; insert into DemoTable(Name) values(&#39;John&#39;); Query OK, 1 row affected (0.50 sec) mysql&gt; insert into DemoTable(Name) values(&#39;Robert&#39;); Query OK, 1 row affected (0.29 sec) mysql&gt; insert into DemoTable(Name) values(&#39;David&#39;); Query OK, 1 row affected (0.54 sec)</pre><p>Display all records from the table using select statement &minus;</p><pre class="prettyprint notranslate">mysql&gt; select *from DemoTable;</pre><h2>Output</h2><pre class="result notranslate">+----+--------+ | Id | Name | +----+--------+ | 1 | John | | 2 | Robert | | 3 | David | +----+--------+ 3 rows in set (0.00 sec)</pre><p>Following is the query to generate field in MySQL SELECT &minus;</p><pre class="prettyprint notranslate">mysql&gt; select Id,Name,&#39;US&#39; AS CountryName from DemoTable;</pre><h2>Output</h2><pre class="result notranslate">+----+--------+-------------+ | Id | Name | CountryName | +----+--------+-------------+ | 1 | John | US | | 2 | Robert | US | | 3 | David | US | +----+--------+-------------+ 3 rows in set (0.00 sec)</pre>
raja
Updated on 30-Jul-2019 22:30:26

Advertisements