How to return static strings in MySQL?


In order to return static strings in MySQL, you can use UNION. Following is the syntax −

select 'yourStringValue1' as yourAliasName
UNION
select 'yourStringValue2' as yourAliasName;

Let us implement the above syntax to return static strings in MySQL. Following is the query −

mysql> select 'HELLO' as staticStringsResult
   -> UNION
   -> select 'MySQL' as staticStringsResult;

This will produce the following output −

+---------------------+
| staticStringsResult |
+---------------------+
| HELLO             |
| MySQL             |
+---------------------+
2 rows in set (0.00 sec)

In some MySQL versions, the above syntax does not work, therefore you need to wrap up with parenthesis −

mysql> (select 'HELLO' as staticStringsResult)
   -> UNION
   -> (select 'MySQL' as staticStringsResult);

This will produce the following output −

+---------------------+
| staticStringsResult |
+---------------------+
| HELLO               |
| MySQL               |
+---------------------+
2 rows in set (0.00 sec)

Updated on: 30-Jul-2019

754 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements