为了在MySQL中返回静态字符串,可以使用UNION。以下是语法-
select 'yourStringValue1' as yourAliasName UNION select 'yourStringValue2' as yourAliasName;
让我们实现以上语法以在MySQL中返回静态字符串。以下是查询-
mysql> select 'HELLO' as staticStringsResult -> UNION -> select 'MySQL' as staticStringsResult;
这将产生以下输出-
+---------------------+ | staticStringsResult | +---------------------+ | HELLO | | MySQL | +---------------------+ 2 rows in set (0.00 sec)
在某些MySQL版本中,以上语法不起作用,因此需要用括号括起来-
mysql> (select 'HELLO' as staticStringsResult) -> UNION -> (select 'MySQL' as staticStringsResult);
这将产生以下输出-
+---------------------+ | staticStringsResult | +---------------------+ | HELLO | | MySQL | +---------------------+ 2 rows in set (0.00 sec)