如何在 MySQL 中返回靜態字串?


為了在 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)

更新於: 30-Jul-2019

1K+ 檢視次數

開啟你的 職業生涯

完成課程獲得認證

立即開始
廣告
© . All rights reserved.