Syntax error How to return static strings in MySQL?

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: 2019-07-30T22:30:25+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements