How to concatenate strings from different columns and an additional string using a MySQL query?

MySQLMySQLi Database

<p>Let us first create a table &minus;</p><pre class="result notranslate">mysql&gt; create table DemoTable ( &nbsp; &nbsp;FirstName varchar(100), &nbsp; &nbsp;LastName varchar(100) ); Query OK, 0 rows affected (0.76 sec)</pre><p>Insert some records in the table using insert command &minus;</p><pre class="result notranslate">mysql&gt; insert into DemoTable values(&#39;Chris&#39;,&#39;Brown&#39;); Query OK, 1 row affected (0.12 sec) mysql&gt; insert into DemoTable values(&#39;Adam&#39;,&#39;Smith&#39;); Query OK, 1 row affected (0.19 sec) mysql&gt; insert into DemoTable values(&#39;Carol&#39;,&#39;Taylor&#39;); Query OK, 1 row affected (0.11 sec) mysql&gt; insert into DemoTable values(&#39;David&#39;,&#39;Miller&#39;); Query OK, 1 row affected (0.12 sec)</pre><p>Display all records from the table using select statement &minus;</p><pre class="result notranslate">mysql&gt; select *from DemoTable;</pre><p>This will produce the following output &minus;</p><pre class="result notranslate">+-----------+----------+ | FirstName | LastName | +-----------+----------+ | Chris &nbsp; &nbsp; | Brown &nbsp; &nbsp;| | Adam &nbsp; &nbsp; &nbsp;| Smith &nbsp; &nbsp;| | Carol &nbsp; &nbsp; | Taylor &nbsp; | | David &nbsp; &nbsp; | Miller &nbsp; | +-----------+----------+ 4 rows in set (0.00 sec)</pre><p>Following is the query to perform concatenation. Here, we have FirstName and LastName. With that an additional string is also concatenated in the beginning for all the string values &minus;</p><pre class="result notranslate">mysql&gt; select concat(&#39;Hello &#39;,FirstName,&#39; &#39;,LastName) from DemoTable;</pre><p>This will produce the following output &minus;</p><pre class="result notranslate">+-----------------------------------------+ | concat(&#39;Hello &#39;,FirstName,&#39; &#39;,LastName) | +-----------------------------------------+ | Hello Chris Brown | | Hello Adam Smith | | Hello Carol Taylor | | Hello David Miller | +-----------------------------------------+ 4 rows in set (0.00 sec)</pre>
raja
Updated on 24-Sep-2019 13:41:30

Advertisements